daltman1967 Posted October 10, 2014 Share Posted October 10, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291563-referencing-arrays/ Share on other sites More sharing options...
Ch0cu3r Posted October 10, 2014 Share Posted October 10, 2014 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? Yes. You can use a variable as the array key. Where is $piccurr defined? Quote Link to comment https://forums.phpfreaks.com/topic/291563-referencing-arrays/#findComment-1493272 Share on other sites More sharing options...
daltman1967 Posted October 12, 2014 Author Share Posted October 12, 2014 $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? Quote Link to comment https://forums.phpfreaks.com/topic/291563-referencing-arrays/#findComment-1493390 Share on other sites More sharing options...
ginerjm Posted October 12, 2014 Share Posted October 12, 2014 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?. Quote Link to comment https://forums.phpfreaks.com/topic/291563-referencing-arrays/#findComment-1493392 Share on other sites More sharing options...
daltman1967 Posted October 13, 2014 Author Share Posted October 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291563-referencing-arrays/#findComment-1493462 Share on other sites More sharing options...
mac_gyver Posted October 13, 2014 Share Posted October 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291563-referencing-arrays/#findComment-1493463 Share on other sites More sharing options...
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.