PDA

View Full Version : PHP: show two array items in while loop


jollyfactory
02-10-2003, 03:59 AM
Just wondering if this is possible. First I have this code


<?php
mysql_connect("localhost","root","**");

//get all main page categories
$sql = mysql_query("SELECT * FROM table");

?>
<table border=1 width=100%>
<?
while ($row = mysql_fetch_array($sql)) {
//place items into variables
$title = $row['title'];
$description = $row['description'];
?>
<tr><td><?echo($title);?></td></tr>
<tr><td><?echo($description);?></td></tr>
<tr><td>=========================</td></tr>
<?

}
?>
</table>


As you can see, it will keep looping through and printing a new table row until the array left in $row is empty.

The link below is how it would sort of output the array
http://resma.net/example1.html

And this is how I want it to look
http://resma.net/example.html
As you can see, the output is the same, its just that each output of the loop is placed in a one of two cells. Just wondering how I could achieve this.

Any help is appreciated.

If you need more clarification, just ask :) and I'll try to be more clearer.

Thanks :)

scoutt
02-10-2003, 10:54 AM
actually theres is a bunch of ways to do that. here is one

<?php
mysql_connect("localhost","root","**");

//get all main page categories
$sql = mysql_query("SELECT * FROM table");

$i = 0;
echo"<table border=\"1\" width=\"100%\">\n";
echo"<tr>\n";
echo"<td>\n";
while ($row = mysql_fetch_array($sql)) {
//place items into variables
$title = $row['title'];
$description = $row['description'];
echo"<table>\n";
echo"<tr><td>($title)</td></tr>\n";
echo"<tr><td>($description)</td></tr>\n";
echo"<tr><td>=========================</td></tr></table>\n";
if ($i%2){
echo"</td>\n";
echo"<td>\n";
} else {
echo"</td></tr>\n";
echo"<tr><td>\n";
}
$i++;
}
?>
</table>

I think that will work so give it a try. it is untested too.

jollyfactory
02-11-2003, 12:23 AM
thankyou so much scoutt!!

I would have never figured this out myself scoutt so thankyou :) Your code is great. Thanks for cleaning up my code too :) (I'm a lazy programmer). I was just wondering though why you used

echo("<html code here> $variable");

instead of
?>
<html code> <?echo($variable);?>
<?

Well your code is excellent and worked great.. just had to change
if ($i%2){
to
if (!($i%2) ){

Thanks :) You rock

scoutt
02-11-2003, 12:31 AM
you're welcome :cool: glad I could help.

the reason why I put html in with php is becasue. tha tis just the way I do it, personal preference I suppose and if you write php thne why not, why insert the <? all the time so it goes in and out of php, just make it all php. :P

jollyfactory
02-11-2003, 01:42 AM
I see :) I guess I'm just too lazy to do that then lol. I think I'll start coding your way though because it looks much more cleaner.

scoutt
02-11-2003, 09:13 AM
yeah that is the worst thing for a programmer, being lazy. when you are lazy it makes the code sloppy and then it is harder to find the errors which when you are sloppy are a lot. :P