0

I am wanting to make the data to show in 2 columns instead of one here is the code I currently use to show the data in 1 column:

    <?php 
include("config.php");
include("opendb.php");
$get_cats = mysql_query("SELECT * FROM `categories` ORDER BY `displayorder`") or die(mysql_error());
$get_info = mysql_query("SELECT * FROM `systeminfo`") or die(mysql_error());
$info = mysql_fetch_array($get_info);
while($cats = mysql_fetch_array($get_cats)) {
$get_rares = mysql_query("SELECT * FROM `rares` WHERE `catid`='".$cats['id']."'") or die(mysql_error());
echo("<h2>".$cats['name']."</h2><br>
<table width=\"100%\" border=\"0\">
<tr>
<td width=\"20%\" style=\"text-align:center\"><b>Image</b></td>
<td width=\"40%\" style=\"text-align:left\"><b>Item Name</b></td>
<td width=\"10%\" style=\"text-align:center\"><b>Value</b></td>
<td width=\"30%\" style=\"text-align:center\"><b>Last Updated</b></td>
</tr>
");
$color1 = $info[stripe1]; 
$color2 = $info[stripe2];
$row_count = 0;
while($rare = mysql_fetch_array($get_rares)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
?>
<tr>
<td width="5%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><img alt="" src="<?php echo("".$info[imagepath]."".$rare['image'].""); ?>"></td>
<td width="20%" style="text-align:left;background-color:#<?php echo $row_color; ?>"><?php echo $rare['name']; ?></td>
<td width="20%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><?php echo $rare['value']; ?> Credits</td>
<td width="10%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><?php echo $rare['lastedited']; ?></td>
</tr>
<?php 
$row_count++;
}
echo("</table><br>

");
}
?>

Currently is shows as:

1
_________

2
_________

3
_________

4
_________

5
_________

6
_________

I would like it to show like this:

1         | 2
_________ | _________
3         | 4
_________ | _________
5         | 6
_________ | _________

2 Answers 2

1

This really doesn't have anything to do with MySQL at all. MySQL's just the source of the data. You'd want something like this:

$record = 0;
while($rare = mysql_fetch_array($get_rares)) {
    if ($record % 2 == 0) {
       echo "<tr>"; // if on an 'even' record, start a new row
    }
    echo "<td>{$rare['something']}</td>";
    $record++;
    if ($record % 2 == 0) {
       echo "</tr>"; // close the row if we're on an even record
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

For what it's worth, I encorporated MarcB's code into your original code. You were already doing the mod with your row count. I think you want something like this:

<?php 
include("config.php");
include("opendb.php");

$get_cats = mysql_query("SELECT * FROM `categories` ORDER BY `displayorder`") or die(mysql_error());
$get_info = mysql_query("SELECT * FROM `systeminfo`") or die(mysql_error());
$info = mysql_fetch_array($get_info);
while($cats = mysql_fetch_array($get_cats)) {
        $get_rares = mysql_query("SELECT * FROM `rares` WHERE `catid`='".$cats['id']."'") or die(mysql_error());
        echo("<h2>".$cats['name']."</h2><br>
                <table width=\"100%\" border=\"0\">
                <tr>
                        <td width=\"20%\" style=\"text-align:center\"><b>Image</b></td>
                        <td width=\"40%\" style=\"text-align:left\"><b>Item Name</b></td>
                        <td width=\"10%\" style=\"text-align:center\"><b>Value</b></td>
                        <td width=\"30%\" style=\"text-align:center\"><b>Last Updated</b></td>
                        <td width=\"20%\" style=\"text-align:center\"><b>Image2</b></td>
                        <td width=\"40%\" style=\"text-align:left\"><b>Item Name2</b></td>
                        <td width=\"10%\" style=\"text-align:center\"><b>Value2</b></td>
                        <td width=\"30%\" style=\"text-align:center\"><b>Last Updated2</b></td>
                </tr>
                ");

        $color1 = $info[stripe1]; 
        $color2 = $info[stripe2];

        $row_count = 0;

        while($rare = mysql_fetch_array($get_rares)) {
                $row_color = ($row_count % 2) ? $color1 : $color2;
                if ($row_count % 2 == 0) {
                        echo "<tr>"; // if on an 'even' record, start a new row
                }

                echo "<td width='5%' style='text-align:center;background-color:#$row_color;'><img alt='' src='{$info[imagepath]}{$rare['image']}'></td>
                                <td width='20%' style='text-align:left;background-color:#$row_color;'>{$rare['name']}</td>
                                <td width='20%' style='text-align:center;background-color:#$row_color;'>{$rare['value']} Credits</td>
                                <td width='10%' style='text-align:center;background-color:#$row_color;'>{$rare['lastedited']}</td>";

                if ($row_count % 2 == 0) {
                        echo "</tr>"; // close the row if we're on an even record
                }

                $row_count++;
        }

        echo "</table><br/>";
}

?>

2 Comments

Hi, I am getting the following error: Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in /home/.../public_html/values/show_rares.php on line 40
Fixed ";" was missing after the last </td>", Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.