So I have a query that I am returning all of the items into a mysql_fetch_array. Now, I know I could write another query and just select the items I need into a seperate query but, is there a way to just filter from the larger query what I want dependent on $_GET?
So, in english the user comes from a hyperlink that has ?id=1 and I peform a while that gets the all the values but, only display the $_GET[‘id’] items in a list
<?php //give ma all values but only echo out list of the $_GET['id'] in the url
while ($row = mysql_fetch_array($result) {
$id = $rowvideo["id"];
$title = $rowvideo["title"];
$length = $rowvideo["length"];
}
echo("<li><a href='#'>". $title." " .$length. "</a></li>");
?>
Hope this makes sense. Thank you all.
If you do not want a second query to get just what you need, a simple-
if-statementin your loop should work:Note that I declared
$getIdoutside of the loop to prevent having to useisset()during every iteration. If you don’t verify if it’s set and attempt to use it it will throw anundefined indexwarning – assuming you haveerror_reportingturned on (with that level enabled).Alternatively, you could use PHP’s
array_filter()on the data after you’ve parsed it all:My personal opinion would be to be more efficient and write the second query though, assuming of course you don’t actually need all of the results when an
idis specified. It would be as simple as: