Jump to content

Recommended Posts

Hello everyone and thank you for having an interest in reading my post  :shy:

 

As the subject stated, I want the output produced by the following code to be split into 2 columns. Here's the code:

 

<?php
// Get categories
$sql = "SELECT catid, catname AS catname
		FROM $t_cats
		WHERE enabled = '1'
		$sortcatsql";		// Version 5.0
$res = mysql_query($sql);

while ($row = mysql_fetch_array($res))
{
?>

<a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br>

<?php
}
?>

 

Can anyone point me in the right direction on how to get this accomplished? I am currently studying web development (so its not like I dont know a little php) but my classes haven't taught us this yet.

 

Anywho, any help will be greatly appreciated  :shy:

Link to comment
https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/
Share on other sites

You could use table rows and columns? 

 


<table>
    <? select and while statement here ?>
    <tr>
        <td>
            <? echo data here ?>
        </td>
        <td>
            <? echo data here ?>
        </td>
    </tr>
    <? end while ?>
</table>

 

 

<?php
// Get categories
$sql = "SELECT catid, catname AS catname
		FROM $t_cats
		WHERE enabled = '1'
		$sortcatsql";		// Version 5.0
$res = mysql_query($sql);
        $num = mysql_num_rows($res);
        $split = (int) $num/2;
        $i = 0;
while ($row = mysql_fetch_array($res))
{
       echo '<div style="width:48%;float:left">';
?>

<a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br>

<?php
          echo ($split == ++$i) ? '</div><div style="width:48%;float:left">' : NULL;
}
      echo '</div>';
?>

 

Let us know!

Thanks for your reply, I just tried it but it didnt work... however it did give me an idea where I might be able to make it work by using a table... let me code it real quick (so you all know what I mean) and if anyone would care to help me with it as always i'll greatly appreciate it! Thanks JCBONES!

How could I make it work so the divider is </td><td>? Here's my attempt at moding your code (I know its bad but like I said im learning)

 

<table><tr><td>
<?php
// Get categories
$sql = "SELECT catid, catname AS catname
		FROM $t_cats
		WHERE enabled = '1'
		$sortcatsql";		// Version 5.0
$res = mysql_query($sql);
        $num = mysql_num_rows($res);
        $split = (int) $num/2;
        $i = 0;
while ($row = mysql_fetch_array($res))
{
?>

<a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br>

<?php
          echo ($split == ++$i) ? '</td><td>' : NULL;
}
?>
</td></tr></table>

It's much easier to write this chunk

 

?>

<a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br>

<?php

 

Like this instead

 

echo '<a href="?view=post&cityid='.$xcityid.'&catid='.$row['catid'].'&shortcutregion='.$_GET['shortcutregion'].'">'.$row['catname'].'</a><br>';

 

As far as your solution goes, I'm not quite following the logic. Here's how I'd do it, using an array rather than MySQL results.

 

<?php 

// Make dummy array
$array = range(0,32);

// This will keep track of how many rows we've echo'd
$i = 0;
// This tells us how many columns
$cols = 3;
$total = count( $array );

echo '<table><tr>';
foreach( $array as $value ) {
// Echo the value in a cell
echo '<td style="border:1px solid black;">'.$value.'</td>';
// Increase our counter by 1 before we check
$i++;
// Check to see if we need to start a new row by using the modulus operator
// It'll give us the remainder of the two numbers divided.
// We check if $i != $total to make sure we don't have a redundant empty row
// at the end
if( ($i % $cols) == 0 && $i != $total ) echo '</tr><tr>'; // Start a new row
}
// Now after the foreach, if we have an odd number of results, we'll have an
// extra empty column we need to fill, we can do this automatically
while( ($i % $cols) != 0 ) {
echo '<td> </td>';
$i++;
}
echo '</tr></table>';

?>

Thanks for your reply, the logic I was trying to do was to modify JC Bones idea but rather than making a split using div tags to instead create a table and then find a way for his code to divide my stuff in 2 by using </td><td>? It might be confusing, but here a second attempt at coding it:

 

<table><tr><td>
<?php
// Get categories
$sql = "SELECT catid, catname AS catname
		FROM $t_cats
		WHERE enabled = '1'
		$sortcatsql";		// Version 5.0
$res = mysql_query($sql);
        $num = mysql_num_rows($res);
        $split = (int) $num/2;
        $i = 0;
while ($row = mysql_fetch_array($res))
{
       echo '<a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br>';
?>



<?php
          echo ($split == ++$i) ? '</td><td>' : NULL;
}
      echo '</td>';
?>
</td></tr></table>

 

I really appreciate your guy's patience with me (hope you guys see that i'm REALLY trying to learn)  :shy:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.