Amster Posted October 21, 2012 Share Posted October 21, 2012 (edited) Hello, I want to use a loop to assign a new variable to a different value from my database. So for example, $item[1] = 'item1' from my database, $item[2] = 'item2' from my database and so forth until 'item100'. I have tried using the following code: $rs_settings = mysql_query("select * from users where id='$_SESSION[user_id]'"); while ($row_settings = mysql_fetch_array($rs_settings)) { for( $i = 1; $i < 100; $i++ ) { $item[$i] = $row_settings['item . $i']; } } And the following code for output: for( $i = 1; $i < 100; $i++ ){ echo $item[$i]; } ?> But it just echoes nothing. Could someone help me with my code so I can get this working? Thanks! Edited October 21, 2012 by Amster Quote Link to comment https://forums.phpfreaks.com/topic/269742-for-loop-to-assign-a-variable/ Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2012 Share Posted October 21, 2012 Your database has fields named `item1`, `item2`, `item3` . . . `item100`? Quote Link to comment https://forums.phpfreaks.com/topic/269742-for-loop-to-assign-a-variable/#findComment-1386775 Share on other sites More sharing options...
Amster Posted October 21, 2012 Author Share Posted October 21, 2012 (edited) I've only made `item1`, `item2` in the database so far but I intend on making the rest. Shouldn't it still be outputting the values of `item1`, `item2` or does it requires me to put every field first in the database? Edit: I tried changing it from <100 to <2 and it still echoes nothing. Edited October 21, 2012 by Amster Quote Link to comment https://forums.phpfreaks.com/topic/269742-for-loop-to-assign-a-variable/#findComment-1386776 Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2012 Share Posted October 21, 2012 What is the purpose of the table; what is the data it will be storing? It sounds like you're about to make a big database design mistake. Quote Link to comment https://forums.phpfreaks.com/topic/269742-for-loop-to-assign-a-variable/#findComment-1386780 Share on other sites More sharing options...
kicken Posted October 21, 2012 Share Posted October 21, 2012 (edited) I've only made `item1`, `item2` in the database so far but I intend on making the rest. That is 100% the wrong way to do it. You should be setting those values as different rows in either that or another table. Eg: create table users ( userid int name varchar(100) ); create table user_items ( userid int, itemvalue varchar(100) ); You'd store the main user details in the users table, then for every item you want to add you create a new row in the user_items table with the user's id and item data. Edited October 21, 2012 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/269742-for-loop-to-assign-a-variable/#findComment-1386781 Share on other sites More sharing options...
Amster Posted October 21, 2012 Author Share Posted October 21, 2012 Ok thank you but how would I go about displaying the quantity I have of each item on my site? Quote Link to comment https://forums.phpfreaks.com/topic/269742-for-loop-to-assign-a-variable/#findComment-1386785 Share on other sites More sharing options...
Barand Posted October 21, 2012 Share Posted October 21, 2012 Use a JOIN on the 2 tables http://www.phpfreaks.com/tutorial/data-joins-unions Quote Link to comment https://forums.phpfreaks.com/topic/269742-for-loop-to-assign-a-variable/#findComment-1386788 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.