php - convert an array into individual strings -
i have following arrays , convert each 1 of them individual strings. in other words, break array individual pieces.
$formatsarray = $_post['formats']; $topicsarray = $_post['topics'];
this because include individual strings in following query "
$resources = "select * resources stage '%".$stage."%' , format '%".$formats."%'"; $run_query = mysqli_query($con, $resources);
this because format expect individual string comparison, such lets assume array ["video", "blogs", "articles"]
, wouldn't work if format compared video,blogs,articles
rather video, blogs or articles.
i hope clear, , clarification, please advise.
all best,
update:
$formats = explode(',', $formatsarray); $topics = explode(',', $topicsarray); $resources = "select * resources stage '%".$stage."%' , format '%".$formats."%' , topic '%".$topics."%' ";
update:
$run_query = mysqli_query($con, $resources); while($row = mysqli_fetch_array($run_query)) { $data[] = array( 'format' => $row['format'], 'title' => $row['title'], 'costs' => $row['cost'], 'stage' => $row['stage'], 'topic' => $row['topic'], 'link' => $row['link'] ); }
update
include('db.php'); $query = 'select * resources '; $query .= 'stage :stage and'; $execute[':stage'] = '%' . $stage . '%'; if(!empty($_post['formats'])){ foreach($_post['formats'] $key => $format) { $query .= 'format :format' . $key . ' , '; $execute[':format' . $key] = '%' . trim($format) . '%'; } } if(!empty($_post['topics'])){ foreach($_post['topics'] $key => $topic) { $query .= 'topic :topic' . $key . ' , '; $execute[':topic' . $key] = '%' . trim($topic) . '%'; } } $query = rtrim($query, ' , '); if(!empty($execute)) { $stmt = $con->prepare($query); $stmt->execute($execute); } else { echo 'you must search something'; } while($row = mysqli_fetch_array($query)) { $data[] = array( 'format' => $row['format'], 'title' => $row['title'], 'costs' => $row['cost'], 'stage' => $row['stage'], 'topic' => $row['topic'], 'link' => $row['link'] ); }
ignoring necessity of prepared statements, do:
$formats = implode('","', $formatsarray); $topics = implode('","', $topicsarray); $resources = "select * resources stage '%".$stage."%' , format in(".$formats.") , topic in(\"".$topics."\") ";
by adding "
before , after each ,
when implode
each array, array become e.g.
video","blogs","articles
so, need add "
beginning , end of each in
list. make final query like:
select * resources stage '%".$stage."%' , format in("video","blogs","articles") , ...
Comments
Post a Comment