sKunKbad Posted July 7, 2007 Share Posted July 7, 2007 I have 3 select queries which i was hoping to condense into a single query. The output is going to be a catagorized list. It works the way it is, and so its not entirely necessary to do this, but I just like learning more, and I'm stuck with this one. Is it possible to condense this into a single query and have loops within loops to do the same thing? $query = 'SELECT * FROM tipsntricks WHERE tipType = "exhaust"'; $result = mysql_query($query); if($result) { echo "<h3>Exhaust Modifications</h3>"; while($row = mysql_fetch_array($result)){ extract($row); echo "Tip Number: " . $tipNum . "<br />"; echo "Tip Title: " . $tipTitle . "<br />"; echo "<img src='" . $tipPicTop . "' /><br />"; } } $query = 'SELECT * FROM tipsntricks WHERE tipType = "suspension"'; $result = mysql_query($query); if($result) { echo "<h3>Suspension Modifications</h3>"; while($row = mysql_fetch_array($result)){ extract($row); echo "Tip Number: " . $tipNum . "<br />"; echo "Tip Title: " . $tipTitle . "<br />"; echo "<img src='" . $tipPicTop . "' />"; } } $query = 'SELECT * FROM tipsntricks WHERE tipType = "engine"'; $result = mysql_query($query); if($result) { echo "<h3>Engine Modifications</h3>"; while($row = mysql_fetch_array($result)){ extract($row); echo "Tip Number: " . $tipNum . "<br />"; echo "Tip Title: " . $tipTitle . "<br />"; echo "<img src='" . $tipPicTop . "' />"; } } Link to comment https://forums.phpfreaks.com/topic/58854-condensing-queries/ Share on other sites More sharing options...
sKunKbad Posted July 7, 2007 Author Share Posted July 7, 2007 OK, I came up with this on my own... but If there aren't any rows I don't want the <h3> to print... $tipTypes = array(exhaust,suspension,engine); foreach ($tipTypes as $tipCat){ $query = "SELECT * FROM tipsntricks WHERE tipType = '$tipCat'"; $result = mysql_query($query); if($result) { echo "<h3>" . ucfirst($tipCat) . "</h3>"; while($row = mysql_fetch_array($result)){ extract($row); echo "Tip Number: " . $tipNum . "<br />"; echo "Tip Title: " . $tipTitle . "<br />"; echo "<img src='" . $tipPicTop . "' /><br />"; } } } Link to comment https://forums.phpfreaks.com/topic/58854-condensing-queries/#findComment-292048 Share on other sites More sharing options...
sKunKbad Posted July 7, 2007 Author Share Posted July 7, 2007 This is as good as it gets: $tipTypes = array(exhaust,suspension,engine); foreach ($tipTypes as $tipCat){ $query = "SELECT * FROM tipsntricks WHERE tipType = '$tipCat'"; $result = mysql_query($query); $num_rows = mysql_num_rows ($result); if ($num_rows != 0){ echo "<h3>" . ucfirst($tipCat) . "</h3>"; while($row = mysql_fetch_array($result)){ extract($row); echo "Tip Number: " . $tipNum . "<br />"; echo "Tip Title: " . $tipTitle . "<br />"; echo "<img src='" . $tipPicTop . "' /><br />"; } } } Anyone got something better? Link to comment https://forums.phpfreaks.com/topic/58854-condensing-queries/#findComment-292053 Share on other sites More sharing options...
bubblegum.anarchy Posted July 8, 2007 Share Posted July 8, 2007 Step through the max three rows returned from the following: SELECT tipType, group_concat(concat_ws('<BR />', concat('Tip Number: ', tip_num), concat('Tip Title: ', tip_title), concat('<IMG src="', tip_pic_top, '" />')) SEPARATOR '<BR />') AS tip_info FROM tipsntricks WHERE tipType IN ('exhaust', 'suspension', 'engine') Update the column names. Link to comment https://forums.phpfreaks.com/topic/58854-condensing-queries/#findComment-292198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.