Hello Amera.
Are you trying to get the news links to appear in 2 columns on your output page?
You can use css or tables to do this. While css is the "correct" way to do it, tables are easier to explain. I'll show you tables.
Code: Select all
<?php
include "config.php";
$sql="SELECT * FROM news ";
$result=mysql_query($sql);
?>
<table><tr>
<?php
$i = 1;
if((mysql_num_rows($result) % 2) == 0){
$complete = false;
}else{
$complete = true;
}
while($rows=mysql_fetch_array($result)){
?>
<td><a href="readnews.php?id=<?php echo $rows['id']; ?>" class="link"><?php echo $rows['title']; ?></a></td>
<?php
if(($i % 2) == 0){
?>
</tr><tr>
<?php
}
$i++;
}
if($complete == true){
?>
<td> </td>
<?php
}
?>
</tr></table>
The code above creates a table on your output page with 2 columns.
You should already understand the <table><tr> code.
The variable $i is used to to check if the row being processed in the while loop is odd or even.
We start setting it = 1.
Next we check to see if the number of rows from your sql is odd or even.
If it is odd, then we need to make sure to complete the table row later on.
You see that I wrapped your link in <td></td> tags.
On the next line, I used % (modulus) to see if the row number ($i) is odd or even.
If it is even, then we end the table row we are on and start a new row.
Next, $i is increased by 1 for the next row.
Once the "while" loop has finished running, we check to see if we need to add an empty table cell (<td>);
Last we close the table row and table (</tr></table>)
If you have challenge translating, tell me and I will try to make it better.
Good Day!