sungpeng Posted May 8, 2009 Share Posted May 8, 2009 $arr = array("somearray" => array($var => $var)); The $var value will keep changing. I need the "$arr" to store all the value that passed the $var. Like say index number 45 passed stored in $arr. index number 60 passed stored in $arr etc. Result $arr = array("somearray" => array(45 => 45, 60 => 60)); Is there a way to do it? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 You can do that, yeah. Is it failing in your code? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 Thank for telling me the above draft code is feasible. Another question link. <?php $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); echo $arr["somearray"][6]; echo $arr["somearray"][13]; echo $arr["somearray"]["a"]; etc ?> Can I have a "while" or "for" loop for (one echo $arr["somearray"][]; statement only) to echo all data in $arr instead of manually one by one key in echo $arr["somearray"][6];, echo $arr["somearray"][13];, echo $arr["somearray"]["a"];. Quote Link to comment Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 foreach ($arr['somearray'] as $key => $val) { echo "{$key} => {$val}<br />"; } foreach Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 foreach ($arr['somearray'] as $key => $val) { $indexnumber ="{$key} => {$val}"; $query=mysql_query("select*from table where id=$indexnumber"); } Check is it possible to code it in this way? Quote Link to comment Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 What signifies an ID number. The key of the array index or the value. Use either or. The echo statement was to just show you it can be done and a rough example of how it is done. If you want the index use $key if you want the value of that index use $val. Simple as that. However, seeing as you want to use them in a MySQL statement, I would suggest looking into array_keys and implode: If you want to use the keys: $query = mysql_query("SELECT * FROM table WHERE id IN('" . implode("', '", array_keys($arr['somearray'])) . "') ORDER BY id"); Of the values: $query = mysql_query("SELECT * FROM table WHERE id IN('" . implode("', '", $arr['somearray']) . "') ORDER BY id"); Using the above two methods (either or) will be way more efficient than looping through your code and running a query for each item. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 <?php session_start(); $indexnumber=$_GET[rid]; $arr = array("somearray" => array($indexnumber => $indexnumber)); $_SESSION['views']=$arr; ?><html> <body><?php $arr=$_SESSION['views']; foreach ($arr['somearray'] as $key => $val) { $query=mysql_query("select*from table where id=$val"); } ?></body> </html> Ok Thank check again why the above code cannot work? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 Ok above is the final result that I need. It still not working so I will break it apart to test. <?php session_start(); $indexnumber=$_GET[rid]; $arr = array("somearray" => array($indexnumber => $indexnumber)); $_SESSION['views']=$arr; ?><html> <body><?php $arr=$_SESSION['views']; echo $arr["somearray"]["indexnumber"]; ?></body> </html> Can I know why the about code not working? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Because you're indexing the array by the variable $indexnumber and not the string. Which do you want? From the topic, I guess you want this: echo $arr['somearray'][$indexnumber]; Although I don't know why you would do that rather than just: echo $indexnumber; Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 I am going to store many $indexnumber in $arr. Then call out one by one in mysql statement. It is still not working. <?php session_start(); $indexnumber=$_GET[rid]; $arr = array("somearray" => array($indexnumber => $indexnumber)); $_SESSION['views']=$arr; ?><html> <body><?php $arr=$_SESSION['views']; echo $arr['somearray'][indexnumber]; ?></body> </html> Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 The problem I have is to store many products in session array then check out, view all products without the person who is using it log in to the account. If you have better solution, tell me. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Storing many $indexnumber in $arr won't make a difference because the last one will just overwrite the other ones. :-\ And indexnumber is a constant. Use $indexnumber. Learn to read. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 How to make it not overwrite the other ones. intended result $arr = array("somearray" => array(5 => 5, 9 => 9, 42 => 42)); It will store all variable passed. Amend Need to store many products in session array then check out, view all products without the person who is using it log in to the account. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Um... unless I misinterpreted, I think you would want to use a database for what you want to achieve. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 8, 2009 Author Share Posted May 8, 2009 Is primary id for mysql be the IP address? I need to assign a temporary name to the user as the user is not log in. I still think is possible to use session array. Just the method of inputing into the array. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 9, 2009 Author Share Posted May 9, 2009 <?php session_start(); $indexnumber=$_GET[rid]; $arr1 = array("somearray" => array($indexnumber => $indexnumber)); $arr2=$_SESSION['views']; $arr=array_combine($arr2, $arr1); $_SESSION['views']=$arr; ?><html> <body><?php $arr=$_SESSION['views']; foreach ($arr['somearray'] as $key => $val) { echo "{$key} => {$val}<br />"; } ?></body> </html> Just checking why about code cannot work? I still think is possible to use session array to store all indexnumber passed. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 9, 2009 Share Posted May 9, 2009 man array_combine Read that. I'm assuming you have no idea what the function does and just applied it blindly. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 9, 2009 Author Share Posted May 9, 2009 <?php session_start(); $indexnumber=$_GET[rid]; $arr1 = array("somearray" => array($indexnumber => $indexnumber)); $arr2=$_SESSION['views']; $arr=array_merge($arr2, $arr1); $_SESSION['views']=$arr; ?><html> <body><?php $arr=$_SESSION['views']; foreach ($arr['somearray'] as $key => $val) { echo "{$key} => {$val}<br />"; } ?></body> </html> It's seen possible to use array merge?? I have seen people use session to store many id and later display all "shortlisted" in one page but I just cannot figure out how? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted May 9, 2009 Author Share Posted May 9, 2009 <?php session_start(); include ("".$_SERVER['DOCUMENT_ROOT']."/config.php"); $query=mysql_query("select * from stats where id='1'" ); $row=mysql_fetch_array($query); $count="$row[counter]"; $_SESSION[$count]=$_GET[rid]; ?><html> <body><?php while($index<=10000) { $arr =$_SESSION[$index]; echo "$arr"; $index++; } ?></body> </html> row[counter] is a counter that will add one once you click on this page, it is within 10000. I don't see any problem in above code but why it cannot work. It time to use database. Quote Link to comment 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.