Another SQL Query Help thread :)

Soldato
Joined
10 Apr 2006
Posts
7,890
Location
North West
I need to output some data from a database, at the moment it is just 1 query that goes through everything and outputs them and styles them with CSS.

Everything that comes out is output into a div with an ID = package.

This div ID has a background-image set to Image_1.jpg

But what I need is the query to output 2 entries at a time into different divs so I can style them differently.

ie

Instead of everything being output to id=package, I need it to output to id=package then the next one to id=package2 then back to id=package etc.

Will upload a picture to show what I mean incase Im not clear! :)

pictures:

This one is how it looks at the moment, without the data in each DIV:
http://www.tomsinclair.co.uk/random/all_right.jpg

This is how I want it to look :
http://www.tomsinclair.co.uk/random/all_ok.jpg

is there a simple way or just outputting 2 entries in 1 query and within that I can style them differently?
 
Last edited:
Code:
$id = 'package1';
$result = mysql_query("SELECT......");
while($row = mysql_fetch_array($result)) {
    echo '<div id="'.$id.'">blah</div>';
    if($id = 'package1') {
        $id = 'package2';
    } else {
        $id = 'package1';
    }
}

?
 
Last edited:
maybe it would have been clearer if i wrote it like this.....

Code:
  if($id = 'package1') {
        $id = 'package2';
    } elseif($id = 'package2') {
        $id = 'package1';
    }

both do exactly the same thing. :)
 
think im a bit naff at php !

Heres the query part of the page:

PHP:
			<?php
			$result = mysql_query("SELECT DISTINCT thingy FROM thingy WHERE thingy = thingy");
			while($row = mysql_fetch_array($result))
			{
			echo "<div id='p_pair'>";
			echo "<div id='p_1'>";
			echo "<a href=package.php?id=" . $row['package_ID'] . ">" . $row['name'] . "</a>";
			echo "<br/>";
			echo "<h1>&pound;" . $row['price'] . "</h1>";
			echo "</div>";
			echo "<div id='p_2'>";
			echo content_truncate($row['description']);
			echo "</div><br/><br/><br/>";
			echo "<div id='p_3'>";
			$imageID = $row['package_ID'];
			$picture_q = "SELECT thingy FROM thingy WHERE thingy = '$imageID' LIMIT 1";
			$picture_r = mysql_query($picture_q) or die('Error, query failed');
			while($row = mysql_fetch_array($picture_r))
			{
			echo "<a href=package.php?id=" . $row['package_ID'] . "><img alt =\"image\" src=\"includes/getimage.php?id=" . $row['picture_ID'] . "\"/></a>";
			echo "<br/><br/>";
			}
			echo "</div></div>";		
			}
			?>

Cant get it to work, it just needs to output everything in the database with alternating "p_pair" to "p_pair_b" for each item
 
Forgot about this problem :D

Can anyone help me out with this, still have it and its annoying me now that I have realised it is still there!
 
Code:
<?php
$div_id = "p_pair";

$result = mysql_query("SELECT DISTINCT thingy FROM thingy WHERE thingy = thingy");
while($row = mysql_fetch_array($result))
{
echo "<div id='" . $div_id . "'>";
echo "<div id='p_1'>";
echo "<a href=package.php?id=" . $row['package_ID'] . ">" . $row['name'] . "</a>";
echo "<br/>";
echo "<h1>&pound;" . $row['price'] . "</h1>";
echo "</div>";
echo "<div id='p_2'>";
echo content_truncate($row['description']);
echo "</div><br/><br/><br/>";
echo "<div id='p_3'>";
$imageID = $row['package_ID'];
$picture_q = "SELECT thingy FROM thingy WHERE thingy = '$imageID' LIMIT 1";
$picture_r = mysql_query($picture_q) or die('Error, query failed');
while($row = mysql_fetch_array($picture_r))
{
echo "<a href=package.php?id=" . $row['package_ID'] . "><img alt =\"image\" src=\"includes/getimage.php?id=" . $row['picture_ID'] . "\"/></a>";
echo "<br/><br/>";
}//while($row = mysql_fetch_array($picture_r))
echo "</div></div>";

if($div_id == "p_pair")
{
$div_id = "p_pair_b";
}
else
{
$div_id = "p_pair";
}

}//while($row = mysql_fetch_array($result))
?>

Single = sets the value
Double == does a comparison

So with the original code all you were doing is resetting the value to the same one each time rather than comparing it.
 
paulsheffII said:
Single = sets the value
Double == does a comparison

So with the original code all you were doing is resetting the value to the same one each time rather than comparing it.

oops. i always include typos to keep people on their toes.... :o
 
psyr33n said:
That echoing is pretty nasty, might want to exit out of PHP to show large chunks of HTML.

Yeah its changed now , just needed that PHP for my simple mind :D

Have to show people the site once its done, nearly finished and then the end of my first paid php job! :D
 
Back
Top Bottom