help with a list

Ask about a PHP problem here.
Post Reply
ud3webdev
Posts: 4
Joined: Thu Jun 28, 2012 1:39 pm

help with a list

Post by ud3webdev »

hey, hopefully some one can help me here. what im trying to do is to take my 2 working systems that use "while" in php into one line.
the first system is the names from tables, gathers all the table names, the second system takes each <li></li> tag and places a color depending if its even or odd in the list.

i am trying to get the 2 systems to echo on the same line together. thanks!
<div id="browser">
<?php
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_row($result)) {
    echo "Table: {$row[0]}\n";//system 1
}
mysql_free_result($result);
$i=0;
while($i<10){
echo "<li class=".lists($i)."> $i second system<br /> Test </li>";
$i++;
} ?>
</div>
the css part if needed
.even{background-color:#CCC}
.odd{background-color:#FFF}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: help with a list

Post by jacek »

Well the obvious thing is to put your
echo "<li class=".lists($i)."> $i second system<br /> Test </li>";
line inside the first while loop, so something a bit like this
<div id="browser">
	<?php
	
	$result = mysql_query("SHOW TABLES FROM $dbname");
	
	if (!$result){
		echo "DB Error, could not list tables\n";
		echo 'MySQL Error: ' . mysql_error();
		exit;
	}
	
	$i = 0;
	
	while ($row = mysql_fetch_row($result)){
		echo '<li class="', (($i++ % 2 == 0) ? 'even' : 'odd'), '">', $row[0], '</li>';
	}
	
	?>
</div>
8-)
Image
ud3webdev
Posts: 4
Joined: Thu Jun 28, 2012 1:39 pm

Re: help with a list

Post by ud3webdev »

Thank you very much!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: help with a list

Post by jacek »

You could also use a for loop to remove a few more lines ;)
        for ($i = 0; $row = mysql_fetch_row($result); ++$i){
                echo '<li class="', (($i % 2 == 0) ? 'even' : 'odd'), '">', $row[0], '</li>';
        }
Then no need to declare $i = 0 above the loop :)
Image
Post Reply