PHP: show two array items in while loop

liunx

Guest
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
<!-- m --><a class="postlink" href="http://resma.net/example1.html">http://resma.net/example1.html</a><!-- m -->

And this is how I want it to look
<!-- m --><a class="postlink" href="http://resma.net/example.html">http://resma.net/example.html</a><!-- m -->
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 :)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.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 rockyou'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. :PI 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.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
 
Back
Top