Page 1 of 1

help with a list

Posted: Thu Jun 28, 2012 2:01 pm
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}

Re: help with a list

Posted: Thu Jun 28, 2012 4:09 pm
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-)

Re: help with a list

Posted: Thu Jun 28, 2012 4:35 pm
by ud3webdev
Thank you very much!

Re: help with a list

Posted: Fri Jun 29, 2012 12:48 pm
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 :)