Jump to content
Larry Ullman's Book Forums

chadwick37

Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by chadwick37

  1. I 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.
  2. I had thought of that too so I tried defining $bg above it but it didn't make any difference.
  3. 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>
  4. 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>'; } }
  5. I am trying to understand how this script works, particularly the email conditional. Here is the code I'm trying to get my head around: //Make the query: $q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id LIMIT 1"; $r = @mysqli_query ($dbc, $q); if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. //Print a message: echo '<p>The user has been edited.</p>'; } else { // If it did not run OK. echo '<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; //Public message. echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message. } } else { // Already registered. echo '<p class="error">The email address has already been registered.</p>'; } } else { //Report the errors. echo '<p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p>'; What I'm not sure I understand is how the script knows to print error for a email address that has already been registered. Is this code basically saying that if the affected rows == 0 but no errors are reported then the only other option is that you used a duplicate email address and so since there are no errors to print it echos the already registered message?
×
×
  • Create New...