Jump to content

[SOLVED] Using multidimensional array


stockton

Recommended Posts

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

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.

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)

 

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).

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.

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.

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?

 

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>

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?

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].

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.