Jump to content
Larry Ullman's Book Forums

Recommended Posts

So I'm trying to apply some of the lessons in the book to a real world application so I am trying to apply the ternary operator just like it was used in the book but in a more complicated script. Here is the script and it isn't working. I think I might have a syntax issue but I can't figure it out. Any ideas?

 

// Start result table printed to browser
if ($_GET['rep1'] == TRUE) {
$numberFields = mysql_num_fields($q); // Find out how many fields we are fetching
if($numberFields) { // Check if we need to output anything
for($i=0; $i<$numberFields; $i++) {
 $keys[] = mysql_field_name($q, $i); // Create array of the names for the loop of data below
 $head[] = (mysql_field_name($q, $i)); // Create the headers for each column, this is the field name in the database
}
$headers = join('</td><td>', $head)."\n"; // Make our first row in the CSV

$data = '';
while($info = mysql_fetch_object($q)) {
 foreach($keys as $fieldName) { // Loop through the array of headers as we fetch the data
  $row[] = (trim($info->$fieldName));
 } // End loop
 $data .= join('</td><td>', $row)."</td></tr>\n<tr bgcolor=\"'.$bg.'\"><td>"; // Create a new row of data and append it to the last row (this line is where I'm having the problem)
 $row = ''; // Clear the contents of the $row variable to start a new row
}
// Output our report
$bg = '#eeeeee'; //Set the initial background color.
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); //Switch the background color.
echo
 '<table>
  <tr bgcolor="' . $bg . '">
   <td>' . $headers . '</td>
  </tr>
  <tr bgcolor="' . $bg . '">
   <td>' . $data . '</td>
  </tr>
 </table>';
}
}

Link to comment
Share on other sites

Here is a piece of the source, you can see what gets output in the table rows. Visually, all the cells are black.

 

<table>
  <tr bgcolor="#ffffff">
   <td>First Name</td><td>Second Name</td><td>Last Name 1</td><td>Last Name 2</td><td>Sponsor First Name</td><td>Sponsor Last Name
</td>
  </tr>
  <tr bgcolor="#ffffff">
   <td>Doris</td><td>Vicenta</td><td>Munguia</td><td>Garcia</td><td>Ore Creek</td><td>Community Church High School Youth</td></tr>
<tr bgcolor="'..'"><td>Paola</td><td>Carolina</td><td>Ochoa</td><td>Paz</td><td>Carolyn</td><td>Carter</td></tr>
<tr bgcolor="'..'"><td>Jehieli</td><td>Elizabeth</td><td>Guevara</td><td>Buruca</td><td>LeAnne</td><td>Ward</td></tr>
<tr bgcolor="'..'"><td>Waldina</td><td>Arely</td><td>Martinez</td><td>Alvarado</td><td>Darla</td><td>Wofford</td></tr>

Link to comment
Share on other sites

Would the problem be that you are defining $bg after this line, instead of before?

$data .= join('</td><td>', $row)."</td></tr>\n<tr bgcolor=\"'.$bg.'\"><td>";

 

I hope this helps,

 

I had thought of that too so I tried defining $bg above it but it didn't make any difference.

Link to comment
Share on other sites

I

I think your closing parenthesis is in the wrong place. In your code, it's:

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

 

But I think it should be:

$bg = ($bg=='#eeeeee') ? '#ffffff' : '#eeeeee';

 

Is this really the answer? I will test it. If it is I am even more confused. This is the code I used from the book and it worked. So why would that parenthesis need to be moved?

 

//Fetch and print all the records…
$bg = '#eeeeee'; //Set the initial background color.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); //Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td>
<td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['dr'] . '</td>
</tr>
';
} //End of WHILE loop.

Link to comment
Share on other sites

 Share

×
×
  • Create New...