0

Hey guys I'm back and I feel like I'm making a really stupid error in my code but I can't seem to find it. What I am trying to do is split data retrieved from a mysql table into two columns inside an html table. So far I got it working...sort of. The problem I am having is the data keeps replicating to both rows in the table versus being an individual entry in each cell. I have used googled and searched these forms and can't seem to see where I am making the error. Below is the code

// Build the table
echo '
<table>
<tr>
';

// Build the headers
for ($x = '1'; $x <= '2'; $x++)
echo '

<td>Truck</td>
<td>Home Base</td>
<td>Client</td>
<td>Job Details</td>
<td>Crew</td>
 ';

 // Close off headers
 echo '</tr>';

 // Fetch the table contents if the rigs are active
 while($row = mysqli_fetch_array($truck_full_query)) {

for ($y = '1'; $y <= '1'; $y++):
    echo '<tr>';
        for ($z = '1'; $z <= '2'; $z++):
                    echo '<td>' . $row['model'] . ' #' . $row['position'] . '<br>' . $row['unit_number'] . '</td>';
                    echo '<td>' . $row['dispatch_location'] . '</td>';
                    // Get client information
            echo '<td>&nbsp;</td>';
            // Get Job Details
            echo '<td>&nbsp;</td>';
            // Get Crew
            echo '<td>&nbsp;</td>';
                endfor;
    echo '</tr>';
    endfor;
      }
     // Close table
    echo '</table>';
6
  • 1
    for ($y = '1'; $y <= '1'; $y++): what's this for? Commented Oct 23, 2013 at 19:58
  • More or less just me fiddling trying to get this to work. Did a straight copy and paste. Commented Oct 23, 2013 at 20:02
  • cargo-cult programming warning: for($y = '1'; $<= '1' ...)`. for loops work on integers, not strings. and whye ven bother with a loop when you're only doing a SINGLE iteration? Commented Oct 23, 2013 at 20:02
  • @MarcB it works on strings too. But the question is what for? Commented Oct 23, 2013 at 20:04
  • where did you copy and paste $y = '1' from? i would actually be interested in looking at the rest of the code there. Commented Oct 23, 2013 at 20:08

1 Answer 1

1
// Build the table
echo '<table><tr>';

// Build the headers
echo '<td>Truck</td><td>Home Base</td><td>Client</td><td>Job Details</td><td>Crew</td>';   
echo '<td>Truck</td><td>Home Base</td><td>Client</td><td>Job Details</td><td>Crew</td>';
echo '</tr>';

$data = mysqli_fetch_all($truck_full_query);
$i = 0;
$t = sizeof($data);
for (; $i < $t; $i += 2) {
    echo '<tr>';
    $left_row = $data[$i];
    echo '<td>' . $left_row['model'] .'</td><td>...</td><td>...</td><td>...</td><td>...</td>';
    $right_row = $data[$i + 1];
    echo '<td>' . $right_row['model'] .'</td><td>...</td><td>...</td><td>...</td><td>...</td>';
    echo '</tr>';
}

echo '</table>

And don't forget to check if the last $right_row exists.

Sign up to request clarification or add additional context in comments.

2 Comments

Looks like this might work but I'm getting a mysqli_fetch_all() error so I might need to play with it a bit to get it to work.
Still no luck getting it to work, but I'll keep fidgeting with it after supper.

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.