I have this code for print a multi columns table from mysql
$k="<table width='100%' border='0' cellpadding='5'><tr>";
$h=mysql_query("select * from news order by id desc limit 0,12");
$col=0;
while($row=mysql_fetch_assoc($h)){
$col++;
$k .="
<td>
Text here
</td>
";
if($col==3){
$k .="</tr><tr>";
$col=0;
}
}
$k .="</tr></table>";
echo $k;
I want to add a random cells inside this table like ad-sense codes and I want the ad-sense code to display once per column.
the output should be like this
<table border="1" width="100%">
<tr>
<td>Title :$row[name]</td>
<td>ADV Code1</td>
<td>Title :$row[name]</td>
</tr>
<tr>
<td>ADV Code2</td>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
</tr>
<tr>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
<td>ADV Code3</td>
</tr>
<tr>
<td>ADV Code4</td>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
</tr>
</table>
How can i do this?
Regards
Piny
Use rand(0,2) which will give you a random number between 0 and 2. Whenever there is a new row, you create a random number, like
Since you are counting the columns in $col, you can go
Then, if a new row is created (as well as before the loop), you assign a new random number:
Or something like this.