Jump to content

Referencing arrays


daltman1967

Recommended Posts

Here's where I am thus far:

 


$link = mysql_connect($connection, $username, $password) or die(mysql_error());
mysql_select_db($database) or die( "Unable to select database");
$query2 = "SELECT * FROM inventory_temp WHERE VehID='$VehID'";
$result2 = mysql_query($query2) or die(mysql_error());
$nt2 = mysql_fetch_array($result2);
...
echo $nt2[username];
echo $nt2[address];
echo $nt2[city];

 

What I need to do is access a range of pictures (Picture01 ... Picture10) that are columns in this database, the information for which is stored in the array $nt2.

 

I've assigned the current picture # (starting at Picture01) to the variable $piccurr, and I'd like to access the array like this:  $nt2[$piccurr] ...

 

However, I get absolutely nothing.  Is it possible to reference the array like this, where the contents of the variable in the brackets is the name of the column I need?  If so, what is the correct coding process?

Link to comment
Share on other sites

$piccurr is defined earlier, starting with "Picture01" and then progressing to "Picture10".  However, the contents "Picture01" produces diddly squat.  If I specify $nt2[Picture01], I get the contents of the Picture01 column for that record.  If I specify instead $nt2[$piccurr], I get nothing.  Am I doing something wrong?

Link to comment
Share on other sites

Try echoing out the value of $piccurr to be sure it is what you think it is.

 

As a matter of practice one should always wrap indices in quotes when specifying them literally.  For ex.   you s/b using $nt2['Picture01'] 

 

And what exactly does this 'diddly squat' look like?  A blank page?  An error message? Anything?.

Link to comment
Share on other sites

Well, actually, the referencing was working precisely as desired... I just didn't realize it.  I checked my variables, and they were correct; however, the data in the TABLE was wrong (which was why I was getting diddly).  There are a few bugs to iron out, but I can handle that.  Thanks for your help anyway.

Link to comment
Share on other sites

your database table design is not normalized. by having columns Picture01 ... Picture10, your code to store, update, delete, and retrieve information is more complicated, since it must find which columns are the ones in use, which is likely the reason you ended up with incorrect data stored in your table.

 

you should have the information for the pictures stored in a separate table, related back to the parent table using the parent's id. this will allow you to store any number of pictures, update or delete any of them, and more importantly, select any number of them and simply loop over the selected rows to display them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.