Search the Community
Showing results for tags 'ternary operator'.
-
After reading this book and practicing, I am creating a site that has more than one main logo, I want them to alternate when the page changes to another page or is refreshed. So I thought of using the ternary operator, but I can't get it to work. Since there is no while loop involved in my header page where this code will go, I guess that's why it doesn't work. I got it to work with php's shuffle function, but that doesn't create the effect I want exactly. Anyways here is where I am at with this, any help to get this working would be appreciated. <header> $imglogo1 = '<img src="images/logo5214.jpg" alt="logo" class="logo2">'; $imglogo2 = '<img src="images/logo5213.jpg" alt="logo" class="logo2">'; $bg = $imglogo1; $bg = ($bg==$imglogo1 ? $imglogo2 : $imglogo1); </header>
-
In chapter 10, in pagination, I am having trouble understanding why the background colors of the table will alternate based on the ternary operator used. I don't understand why $bg will change from #eeeeee to #ffffff. Before the ternary operator $bg is set to #eeeeee, and I don't see how that will change based on the code. Here is the code. $q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users ORDER BY registration_date ASC LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); echo '<table align="center" cellspacing="0" cellpadding="5" width="75%"><tr><td align"left"><b>Edit</b></td><td align="left"><b>Delete</b></td>'; echo '<td align="left"><b>Last Name</b></td><td align="left"><b>First Name</b></td><td align="left"><b>Date Registered</b></td></tr>'; $bg = '#eeeeee'; while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); 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'] . '">'; echo 'Delete</a></td><td align="left">' . $row['last_name'] . '</td><td align="left">' . $row['first_name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>'; } echo '</table>';