brown2005 Posted June 13, 2013 Share Posted June 13, 2013 Hi, I want to have an empty array then when I load a page it will get the id of the mysql data an insert this into the array, up to a maximum of 6 times.. I have no ideas how to set or update arrays, so any help will be much appreciated Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/ Share on other sites More sharing options...
ginerjm Posted June 13, 2013 Share Posted June 13, 2013 You want to learn about arrays in php?? Try the php manual? Or is it something else you want? Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435818 Share on other sites More sharing options...
brown2005 Posted June 13, 2013 Author Share Posted June 13, 2013 $numbers = array(); so now say I have while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ $id = $row['id']; } how do I add that $id into the $numbers array? Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435820 Share on other sites More sharing options...
ginerjm Posted June 13, 2013 Share Posted June 13, 2013 http://us.php.net/manual/en/funcref.php Search box at top Type in "array" All kinds of useful things there you are going to want to know about. Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435822 Share on other sites More sharing options...
brown2005 Posted June 13, 2013 Author Share Posted June 13, 2013 $_SESSION['numbers'] = array(); array_push($_SESSION['numbers'], $id); print_r($_SESSION['numbers']); So I have this code, which gives me the array: Array ( [0] => 9 ) but what I want to do is store this, so that every time it refreshes it will keep the last time plus the new number? Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435833 Share on other sites More sharing options...
ginerjm Posted June 13, 2013 Share Posted June 13, 2013 So I'm gathering you are using your page to capture one id at a time and want to save them all, up to 6. When you process each page submitted, add the new $id to the session var that you have created. Check how many there are and if less than 6, repeat. After six, I don't know what you want to do. Do I have to write the code? Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435836 Share on other sites More sharing options...
brown2005 Posted June 13, 2013 Author Share Posted June 13, 2013 yeah thats exactly what I want to do, but the code I currently have only adds a new $id each time, so always one id, not extra Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435837 Share on other sites More sharing options...
ginerjm Posted June 13, 2013 Share Posted June 13, 2013 Well, you're using a session variable so each new entry should be saved from each iteration. Of course, if you execute that line: $_SESSION['numbers'] = array(); each time thru, then what do you think that is doing? Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435839 Share on other sites More sharing options...
brown2005 Posted June 13, 2013 Author Share Posted June 13, 2013 creating a new array each time? Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435844 Share on other sites More sharing options...
ginerjm Posted June 13, 2013 Share Posted June 13, 2013 Tada!! So only create the array when you load the page for the first time. if (!isset($_SESSION['numbers'])) $_SESSION['numbers'] = array(); Then each time you process your $_POST and add an entry, do: if (count($_SESSION['numbers']) < 6) { (redisplay your page and exit) } Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435846 Share on other sites More sharing options...
brown2005 Posted June 13, 2013 Author Share Posted June 13, 2013 awesome thank you very much for all your help Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435847 Share on other sites More sharing options...
ginerjm Posted June 13, 2013 Share Posted June 13, 2013 HTH. Be sure to mark this post as answered. Link to comment https://forums.phpfreaks.com/topic/279127-using-an-array-and-inserting-ids-from-a-mysql-table-until-the-array-has-6-items/#findComment-1435849 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.