stockton Posted June 8, 2008 Share Posted June 8, 2008 I have a database of information which I retrieve via:- while ($row = mssql_fetch_array($rs)) { $TransactionDate = $row['DateWon']; $MemberNumber = $row['MemberNumber']; $TicketNumber = $row['TicketNumber']; $UserID = $row['UserID']; $LQS = "Select * from Users WHERE UserNum = ".$UserID; $sr = mssql_query($LQS, $link); while ($row = mssql_fetch_array($sr)) { $First = $row['FirstName']; $Surname = $row['LastName']; } $User = $First." ".$Surname; } the data of which I need to use later in my script. Therefore I felt if I could save the retrieved data in an array I could extract it later when I need to use it. Most of the tutorials I have looked at only have one item in their arrays and I want 5. Maybe it is just because I am an old time C programmer but I would have thought this would be easy but so far I have had no joy. BTW Where I want to use this data is in an html table similar to the following:- <tr bgcolor="#C0A062" onMouseover=this.bgColor="#FFF7DF" onMouseout=this.bgColor="#C0A062" > <td align=\"center\"><?php echo $TransactionDate ?></td> <td align=\"center\"><?php echo $MemberNumber ?></td> <td align=\"center\"><?php echo $TicketNumber ?></td> <td align=\"center\"><?php echo $User ?></td> </tr> Suggestions please. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/ Share on other sites More sharing options...
GingerRobot Posted June 8, 2008 Share Posted June 8, 2008 Something like this? $array = array(); while ($row = mssql_fetch_array($sr)) { $array[$row['UserNum']] = array('First Name'=>$row['FirstName'],'Last Name'=>$row['LastName']); } echo '<pre>'.print_r($array,1).'</pre>'; Of course, if you just have two things to store in your array, it would be easier to store it as First name => Last name. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-560412 Share on other sites More sharing options...
stockton Posted June 8, 2008 Author Share Posted June 8, 2008 Thank you for your reply. An item in your reply that I do not understand is what the => means. I understand = and >= but please tell me what the => in the context of your suggestion means. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-560447 Share on other sites More sharing options...
paul2463 Posted June 8, 2008 Share Posted June 8, 2008 ok here we go, will try for you array('First Name'=>$row['FirstName'],'Last Name'=>$row['LastName']); means 1. create an array object 2. lets say that $row['FirstName'] is "Fred" 3. create a key called 'First Name' and make the value of that key equal Fred (that's what => means) Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-560450 Share on other sites More sharing options...
GingerRobot Posted June 8, 2008 Share Posted June 8, 2008 I always read it as 'goes to', but then that's probably just me! So yes, it's just used to give a key a particular name, rather than just having numerical keys. You use it when creating an array: $array = array('mykey'=>'myvalue'); Therefore, to access myvalue, you would need to use $array['mykey']. And you also use it if you wish to access the key as well as the value in a foreach loop. foreach($array as $k=>$v){ echo 'The key is: '.$k.' and the value is: '.$v; } If you just want the value, you can do this: foreach($array as $v){ echo 'The value is: '.$v; } Sometimes people confuse the operator with this: -> which is completely different. -> This is used to reference properties or methods of classes (see here). Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-560455 Share on other sites More sharing options...
stockton Posted June 8, 2008 Author Share Posted June 8, 2008 Thank you. I did not know that the key was being given a name by the =>, but now I know....:-) Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-560469 Share on other sites More sharing options...
stockton Posted June 9, 2008 Author Share Posted June 9, 2008 What I have done is:- $Winners = array(); while ($row = mssql_fetch_array($sr)) { $Winners[$row['UserNum']] = array('First Name'=>$row['FirstName'], 'Last Name'=>$row['LastName']); $Count++; } echo '<pre>'.print_r($Winners,1).'</pre>'; but all I get printed is :- Array { } Please tell me what I have misunderstood. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-560979 Share on other sites More sharing options...
GingerRobot Posted June 9, 2008 Share Posted June 9, 2008 Well, there's nothing actually wront with the above; either $row is empty or the field names aren't as listed. Never having used mssql, im guessing a touch. But either way, try this: <?php $Winners = array(); if($sr !== FALSE){ if(mssql_num_rows($sr) > 0){ while ($row = mssql_fetch_array($sr)){ echo '<pre>'.print_r($row,1).'</pre>'; $Winners[$row['UserNum']] = array('First Name'=>$row['FirstName'], 'Last Name'=>$row['LastName']); $Count++; } echo '<pre>'.print_r($Winners,1).'</pre>'; }else{ echo 'No results returned'; } }else{ echo 'Query failed'; } ?> It should narrow it down for you. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-560984 Share on other sites More sharing options...
stockton Posted June 9, 2008 Author Share Posted June 9, 2008 I have eventually got correct data in my array as follows:- $Count = 0; // Here we populate array of previous winners $Winners = array(); while ($row = mssql_fetch_array($rs)) { echo '<pre>'.print_r($row,1).'</pre>'; $Winners[$row[$Count]] = array('Date Won'=>$row['DateWon'], 'MemberNumber'=>$row['MemberNumber'], 'TicketNumber'=>$row['TicketNumber'], 'UserID'=>$row['UserID'], 'RoundCounter'=>$row['RoundCounter']); $Count++; } echo '<pre>'.print_r($Winners,1).'</pre>'; but it displays incorrectly:- Array ( [1] => Array <------- Looks OK except I would expect to start @ 0 ( [Date Won] => 2008-01-01 00:00:00 [MemberNumber] => 2020119011 [TicketNumber] => 0100018481 [userID] => 37 [RoundCounter] => 1 ) [2020119011] => Array <------- Where did this come from ( [Date Won] => 2008-01-02 00:00:00 [MemberNumber] => 2020119011 [TicketNumber] => 0100018481 [userID] => 37 [RoundCounter] => 2 ) [WCC] => Array <------- Where did this come from ( [Date Won] => 2008-01-03 00:00:00 [MemberNumber] => 2020119011 [TicketNumber] => 0100018481 [userID] => 37 [RoundCounter] => 3 ) and finally how do I extract this data from my array? Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561029 Share on other sites More sharing options...
stockton Posted June 9, 2008 Author Share Posted June 9, 2008 OK I got part of it:- $Winners[$row[$Count]] should read $Winners[$Count]. Now how do I extract the data? Would it be something like $Winners[0][Date Won] and $Winners[1][Date Won] etc? Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561042 Share on other sites More sharing options...
stockton Posted June 9, 2008 Author Share Posted June 9, 2008 In fact what I need to populate from that array is a table as follows:- <tr bgcolor="#C0A062" onMouseover=this.bgColor="#FFF7DF" onMouseout=this.bgColor="#C0A062" > <td align=\"center\"><?php echo $Winner[0]['Date Won'] ?></td> <td align=\"center\"><?php echo $Winner[0]['MemberNumber'] ?></td> <td align=\"center\"><?php echo $Winner[0]['TicketNumber'] ?></td> <td align=\"center\"><?php echo $Winner[0]['UserID'] ?></td> </tr> Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561051 Share on other sites More sharing options...
stockton Posted June 9, 2008 Author Share Posted June 9, 2008 I now have:- <?php for ($Nommer = 0; $Nommer <= $Count; $Nommer++) { echo "<tr bgcolor='#C0A062' onMouseover=this.bgColor='#FFF7DF' onMouseout=this.bgColor='#C0A062'> <td align='center'>.$Winner[$Nommer][Date Won].</td> <td align='center'>.$Winner[$Nommer][MemberNumber].</td> <td align='center'>.$Winner[$Nommer][TicketNumber].</td> <td align='center'>.$Winner[$Nommer][userID].</td> </tr>\n"; } ?> but it displays the key not the value? Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561092 Share on other sites More sharing options...
GingerRobot Posted June 9, 2008 Share Posted June 9, 2008 You've lost me a little. How about posting your updated code? Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561190 Share on other sites More sharing options...
keeB Posted June 9, 2008 Share Posted June 9, 2008 This may be a bit out of context, but I'd consider revising your sql statement to use a JOIN. You're needlessly pulling in $rs just to get a userId. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561195 Share on other sites More sharing options...
stockton Posted June 9, 2008 Author Share Posted June 9, 2008 Ben, The code to populate the array looks like:- $Count = 0; // Here we populate array of previous winners $Winners = array(); while ($row = mssql_fetch_array($rs)) { $Winners[$Count] = array('Date Won'=>$row['DateWon'], 'MemberNumber'=>$row['MemberNumber'], 'TicketNumber'=>$row['TicketNumber'], 'UserID'=>$row['UserID'], 'RoundCounter'=>$row['RoundCounter']); $Count++; } echo '<pre>'.print_r($Winners,1).'</pre>'; and the print_r gives me the correct data as follows:- Array ( [0] => Array ( [Date Won] => 2008-01-01 00:00:00 [MemberNumber] => 2020119011 [TicketNumber] => 0100018481 [userID] => 37 [RoundCounter] => 1 ) [1] => Array ( [Date Won] => 2008-01-02 00:00:00 [MemberNumber] => 2020119011 [TicketNumber] => 0100018481 [userID] => 37 [RoundCounter] => 2 ) [2] => Array ( [Date Won] => 2008-01-03 00:00:00 [MemberNumber] => 2020119011 [TicketNumber] => 0100018481 [userID] => 37 [RoundCounter] => 3 ) [3] => Array ( [Date Won] => 2008-01-01 00:00:00 [MemberNumber] => 2 [TicketNumber] => 0100018481 [userID] => 37 [RoundCounter] => 4 ) ) Now how do I populate my HTML page? The closest I have got is:- <?php for ($Nommer = 0; $Nommer < $Count; $Nommer++) { $Name = Get_Member_Name($Winner[$Nommer][MemberNumber]); echo "<tr bgcolor='#C0A062' onMouseover=this.bgColor='#FFF7DF' onMouseout=this.bgColor='#C0A062'> <td align='center'>.$Winner[$Nommer][Date Won].</td> <td align='center'>.$Winner[$Nommer][MemberNumber].</td> <td align='center'>.$Name.</td> <td align='center'>.$Winner[$Nommer][TicketNumber].</td> <td align='center'>.$Winner[$Nommer][userID].</td> </tr>\n"; } ?> but this displays the key not the data/value. Date MemberNumber Member Name TicketNumber User .[Date Won]. .[MemberNumber]. .Client [] not found!. .[TicketNumber]. .[userID]. .[Date Won]. .[MemberNumber]. .Client [] not found!. .[TicketNumber]. .[userID]. .[Date Won]. .[MemberNumber]. .Client [] not found!. .[TicketNumber]. .[userID]. .[Date Won]. .[MemberNumber]. .Client [] not found!. .[TicketNumber]. .[userID]. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561247 Share on other sites More sharing options...
GingerRobot Posted June 9, 2008 Share Posted June 9, 2008 Try this: <?php for ($Nommer = 0; $Nommer < $Count; $Nommer++){ $Name = Get_Member_Name($Winners[$Nommer]['MemberNumber']); echo "<tr bgcolor='#C0A062' onMouseover=this.bgColor='#FFF7DF' onMouseout=this.bgColor='#C0A062'> <td align='center'>{$Winners[$Nommer]['Date Won']}</td> <td align='center'>{$Winners[$Nommer]['MemberNumber']}</td> <td align='center'>{$Name}</td> <td align='center'>{$Winners[$Nommer]['TicketNumber']}</td> <td align='center'>{$Winners[$Nommer]['UserID']}</td> </tr>\n"; } ?> 1.) You were using $Winner instead of $Winners. 2.) PHP finds the echoing of multidimensional arrays ambigous. For example: //given this: echo "$array[0][0]"; //did you mean this: echo $array[0].'[0]'; //that is, the value of $array[0] followed by the literal string [0] //or this: echo $array[0][0]; //the value of the array at index [0][0] You can therefore place braces around the variable to remove ambiguity. 3.) You should place quotes around keys if they are strings. Changing your error_reporting to E_ALL would have helped you spot that much quicker as it would have warned you of undefined variables and constants. See here and the links on that page for more details. Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561364 Share on other sites More sharing options...
stockton Posted June 10, 2008 Author Share Posted June 10, 2008 Thank you again and sorry that you needed to point out my stupid error of using $Winner rather than $Winners Link to comment https://forums.phpfreaks.com/topic/109253-solved-using-multidimensional-array/#findComment-561806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.