How to split mysql into 2 columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amera
Forum Newbie
Posts: 9
Joined: Fri Sep 19, 2008 6:49 am

How to split mysql into 2 columns

Post by amera »

Greetings ,i am new to php & my tongue is not english , i want to split mysql result into 2 columns , searched the net but did not find any thing help .

Code: Select all

<?php
include "config.php";
$sql="SELECT * FROM news ";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
<a href="readnews.php?id=<? echo $rows['id']; ?>" class="link"><? echo $rows['title']; ?></a>
}
I want it like this
--------- ---------
column 1 column 2
--------- ---------
May you help me to understand how to do it ?
Thank you
User avatar
LuckyShot
Forum Newbie
Posts: 13
Joined: Thu Sep 25, 2008 5:02 pm
Location: Barcelona

Re: How to split mysql into 2 columns

Post by LuckyShot »

Hi amera,

Are you trying to separate a text string into two?
You will need some character/s to split them (a space, a comma...).

Have a look at the explode() function: http://www.php.net/explode

Code: Select all

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
amera
Forum Newbie
Posts: 9
Joined: Fri Sep 19, 2008 6:49 am

Re: How to split mysql into 2 columns

Post by amera »

HI LuckyShot
Thank you for your effort , but it gives text only not link , as you see in my above code there is <a> tag .
[code]<a href="readnews.php?id=<? echo $rows['id']; ?>" class="link"><? echo $rows['title']; ?></a>
mistymorningglory
Forum Newbie
Posts: 5
Joined: Fri Sep 26, 2008 4:27 pm

Re: How to split mysql into 2 columns

Post by mistymorningglory »

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>&nbsp;</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!
amera
Forum Newbie
Posts: 9
Joined: Fri Sep 19, 2008 6:49 am

Re: How to split mysql into 2 columns

Post by amera »

Dear mistymorningglory
Thank you for your kindness , it is what i need but the output page into the template gives the links on 2 levels every column has different level .
If there is any tutorial about While css i need to read it Please .
Your help is highly appreciated , thank you mistymorningglory .
Post Reply