chadwick37 0 Posted January 24, 2012 Report Share Posted January 24, 2012 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>'; } } Quote Link to post Share on other sites
Dark Prince 5 Posted January 24, 2012 Report Share Posted January 24, 2012 when you run the script what error do you get? 1 Quote Link to post Share on other sites
chadwick37 0 Posted January 24, 2012 Author Report Share Posted January 24, 2012 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> Quote Link to post Share on other sites
Josee 38 Posted January 25, 2012 Report Share Posted January 25, 2012 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, 1 Quote Link to post Share on other sites
chadwick37 0 Posted January 25, 2012 Author Report Share Posted January 25, 2012 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. Quote Link to post Share on other sites
HartleySan 826 Posted January 27, 2012 Report Share Posted January 27, 2012 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'; 1 Quote Link to post Share on other sites
Larry 429 Posted January 27, 2012 Report Share Posted January 27, 2012 Thanks for your help, HartleySan! Quote Link to post Share on other sites
chadwick37 0 Posted January 27, 2012 Author Report Share Posted January 27, 2012 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. Quote Link to post Share on other sites
HartleySan 826 Posted January 27, 2012 Report Share Posted January 27, 2012 Actually, I don't know if that's the answer until you test it, but I always put the closing right parenthesis before the question mark, so please give it a try and let us know what happens. Quote Link to post Share on other sites
Paul Swanson 104 Posted January 27, 2012 Report Share Posted January 27, 2012 I think your problem is that you haven't defined $bg before you use it in this line: $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) 1 Quote Link to post Share on other sites
Antonio Conte 426 Posted February 2, 2012 Report Share Posted February 2, 2012 Your logic is wrong. That cannot switch like that. Try an equation with modolus ( x % y = 0) Quote Link to post Share on other sites
Antonio Conte 426 Posted February 2, 2012 Report Share Posted February 2, 2012 Sorry, of course you can. Did not spot the query. Jonathan is fight about the parantheses though. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.