Change image when new option selected in PHP?
This probably will also require Javascript. I am using this code:
What it does is draws from a MySQL database where an “avatars” table is set. The “avatars” table includes fields “id” (INT), “title” (VARCHAR) and “url” (VARCHAR).
Basically, the script draws the rows from the table. It puts the titles in an HTML select form as options.
{code block snipped}
When an option is selected, I would like the image that is selected to be shown, without a new page having to be loaded.
This is very easy to do with JavaScript and DOM manipulation, but based on the user’s code, it requires a bit more work that previous blog entries I’ve made on images and JavaScript.
Let’s start with looking at the original code block.
I was going through code today, trying to make some things more compact. The application worked without problems, so I knew that mysql error statements were superfluous.
This is the type of statement I was changing, I figured that I’d remove the seemingly unnecessary $result2 variable
$result2=mysql_query($carts) or die(mysql_error());<br />while($row2=mysql_fetch_array($result2)){<br />echo"<option>$row2[0]</option>";<br />}
so this is what I changed it into (I basically replaced where it said $result2, to what $result2 had contained, and removed the error check)
but this code returned infinite loops, much to my surprise. Why is it doing this? is there a way around it?
Recently, I wrote about how PHP’s weak data types often can lead to problems for new programmers who don’t understand the difference between null, empty and zero-length variables. Here’s another opportunity to expose why strong data typing is essential for best programming practices, and to show how PHP’s weak data types — normally, a source of comfort for beginning programmers — can be the source of extensive frustration.
A common problem faced by new programmers is understanding the difference between null, empty and zero-length variables, especially when working with database records.
While, for most intents and purposes, the three things have the same effect — either you have some data you can work with, or you don’t — they arise from different circumstances. Understanding how null, empty and zero-length are different can help you avoid data errors in your programs.
The short version is this: If a variable simply doesn’t exist — usually because it hasn’t been declared, but sometimes because it hasn’t been assigned a value — it’s null. If the variable exists but contains no data, it’s empty. And if a structured data variable, such as an array, exists but doesn’t contain any items, it’s zero-length.
An analogy is in order.
Think of a database as a house. A house has rooms, and in each of these rooms are furniture and accessories that are appropriate to that room.
For example, the kitchen has a stove, refrigerator and sink. The bedroom has a dresser, bed and armoire. The living room has a couch, television and coffee table.
The firing of Lane Kiffin as head coach of the Oakland Raiders is hardly unexpected. But I really didn’t expect Al Davis, the owner, to hold that press conference, following the firing, to assassinate Kiffin’s character publicly.
A lot of people have criticisms of Davis, many of them valid; e.g., he’s running his franchise into the ground, he’s out of touch with the modern game, he’s old and ugly. Some claim Davis is unbalanced, but his press conference certainly should have dispelled that notion.
Davis is entirely lucid. Which makes what he did during that press conference all the worse. No matter how much you hate an employee, you don’t throw him under the bus. You sever ties and that’s it. Any time you say something negative about an employee or business partner in public, you’re making yourself look bad, not the other guy.