Jump to content

Condensing queries


sKunKbad

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.