0

I am trying to do something that should be trivial. I have a database table that I want to load into an array so that I can use the elements later on in my code. What I dont understand is how to put the elements into a simple 2 dimensional array so that I can get elements to the elements lik $tbl[$r][$c]

So basically I would like to do something like:

           $tbl=array();
           $rowno='0';
           while ($row = mysql_fetch_assoc($result))
           {
               $Type=$row['Type'];
               $Category=$row['Category'];
               $SubCategory=$row['SubCategory'];
               $InflationRate=$row['InflationRate'];
               $tbl[$rowno][0]=$rowno;
               $tbl[$rowno][1]=$Category;
               $tbl[$rowno][2]=$SubCategory;
               $tbl[$rowno][3]=$InflationRate;
               $tbl[$rowno][4]=0;
               $rowno++;
           }
5
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by Manda.Ravi
0

Hi hlamster,

If I understand you right, this should help.

<?php
// Initialise the main array and counter.
$main_array = array();
$rowno='0';

while ($row = mysql_fetch_assoc($result))
{
// Set the variables.
    $Type=$row['Type'];
    $Category=$row['Category'];
    $SubCategory=$row['SubCategory'];
    $InflationRate=$row['InflationRate'];

// Push all the variables onto the end of the main array.
    array_push($main_array, $Type, $Category, $SubCategory, $InflationRate);

// Create the sub array.
    $tbl = array($rowno, $Category, $SubCategory, $InflationRate);

// Push the sub array onto the end of the main array.
    array_push($main_array, $tbl);

// Increment the counter.
    $rowno++;
}
?>
0

I removed line 15 as it seemed to give me double values. Dumping it with print_r seems to show that everything is in the array ... but how can I retrieve it. I tried the following but it just gave me junk:

        for ($i=0; $i<$nr; $i++)
        {
           print "main_array[$i] = ";
           for ($j=0; $j<5; $j++) print "$main_array[$i][$j] ";
           print "<br />\n";
        }

I am sure there is a simple way to do this - just can't figure it out.

Thanks/Hal

0

Hi again.

The reason there were duplicate values is $tbl is an array (the mulit part in multi-dimensional) containing all the values again. I assumed you wanted to keep it that way.

You are almost on the right track with retrieving the values, but you need a FOREACH loop instead of a FOR loop.

<?php
foreach ($main_array as $value){
    $value[0] = $type;
    $value[1] = $category;
    $value[2] = $subcategory;
    $value[3] = $InflationRate;
    $value[4] = $tbl; // This is also an array so you need to add another FOREACH loop here to retrieve it's values.

    // Do something here with this set of values before moving on to the next loop.
}
?>
0

I can create the array. It looks like the following using print_r:

Array ( [0] => Array ( [0] => 1 [1] => Auto [2] => Gas [3] => 3.75 [4] => 0 ) [1] => Array ( [0] => 2 [1] => Auto [2] => Maintenance [3] => 3.75 [4] => 0 ) [2] => Array ( [0] => 3 [1] => Auto [2] => Insurance [3] => 3.75 [4] => 0 ) [3] => Array ( [0] => 4 [1] => Auto [2] => Car Payments [3] => 0 [4] => 0 ) [4] => Array ( [0] => 5 [1] => Auto [2] => *Other [3] => 3.75 [4] => 0 ) [5] => Array ( [0] => 6 [1] => Clothing [2] => Children [3] => 3.75 [4] => 0 ) [6] => Array ( [0] => 7 [1] => Clothing [2] => Wife [3] => 3.75 [4] => 0 ) [7] => Array ( [0] => 8 [1] => Clothing [2] => Husband [3] => 3.75 [4] => 0 ) [8] => Array ( [0] => 9 [1] => Clothing [2] => *Other [3] => 3.75 [4] => 0 ) [9] => Array ( [0] => 10 [1] => Food [2] => Supermakets [3] => 3.75 [4] => 0 ) [10] => Array ( [0] => 11 [1] => Food [2] => Meals Out with Kids [3] => 3.75 [4] => 0 ) [11] => Array ( [0] => 12 [1] => Food [2] => Meals Out w/o Kids [3] => 3.75 [4] => 0 ) [12] => Array ( [0] => 13 [1] => Food [2] => *Other [3] => 3.75 [4] => 0 ) [13] => Array ( [0] => 14 [1] => Gifts [2] => Children [3] => 3.75 [4] => 0 ) 

How do I get to the 2nd element in the 3rd row of the array (i.e. $main_array[2][1] ='Auto') and the 3rd element in the 3rd row (i.e.$ main_array[2][2] = 'Insurance')

I tried the following but it didn't work:

        foreach ($main_array as $value)
        {
           for ($j=0; $j<5; $j++) print "$value[j] ";
           print "<br />\n";
        }

Thanks. I know I should know this but it is driving me nuts.
Hal

0

Hi Hal,

It may be easiest to break the main array into smaller arrays, then retrieve the value.

// Break the main array into separate arrays.
$first_array = $main_array[0];
$second_array = $main_array[1];
$third_array = $main_array[2];
$tenth_array = $main_array[3];


// Choose the element you want as usual.
echo $third_array[1]; // will display the second element from the third array (Auto).
echo $third_array[2]; // will display the third element from the third array (Insurance).
echo $tenth_array[0]; // will display the fourth element from the tenth array (3.75).

Edited by Zagga

0

If you do this:

while($data = mysql_fetch_array($result)){
    $myarray[] = $data;
}
...
$x = 1;
foreach($myarray as $m){
    echo "Record #$x: Second value = {$m[1]}, Third value = {m[2]}<br />";
    $x++;
}

Is that what you need?

0
$tbl=array();
           //$rowno=0;//quotes makes it a string, we want a number/integer
           while ($row = mysql_fetch_assoc($result))
           {
                //$row is a associative array already, why bother redefining it
                //$tbl[$rowno] = $row;
                //you dont even need the $rowno if it starts from 0 and goes up by 1 each loop
                //arrays in php auto assign a key of 0 going up by 1
                $tbl[] = $row;
               //$rowno++;
           }
           foreach($tbl as $v){
                echo "<tr>\r\n";
                //echo set fields out
                //echo "<td>{$v['Type']}</td>\r\n";
                //echo "<td>{$v['Category']}</td>\r\n";
                //echo "<td>{$v['SubCategory']}</td>\r\n";
                //echo "<td>{$v['InflationRate']}</td>\r\n";
                //or loop through all available fields
                foreach($v as $key=>$value){
                    echo "<td>{$key} -> {$value}</td>\r\n";
                }
                echo "</tr>\r\n";
           }

What i do when working with data from tables is i create an array with the unique id from the database being the key of the array ie. at this point:

$clientArray = array();
while ($row = mysql_fetch_assoc($result)){
    $clientArray[$row['client_id']] = $row;
}

Then later you can reference any client you want with it for data - echo $clientArray[126]['contract_start']; or even loop through another array of saved client id's:

$myClients = array(3,12,33,75);
foreach($myClients as $v){
    echo $clientArray[$v]['first_name'].' '.$clientArray[$v]['surname'].' - '.$clientArray[$v]['company'].' <a href="view-client.php?id='.$v.'">View client</a>'."\r\n";
}

Edited by Biiim

0

In JSP, I tried to insert values for a textfield where the textfield runs according to condition in WHILE loop.
But i am unable to read all those values from the textfield insert those into Oracle database.

1.     <%
2.      while (rs.next()) {
3.    %>
4.     <TD>
5.      <input type="text" size="2" name="classesattended" id="classesattended" value="1" />
6.      </TD>
7.      
8.     * //for inserting i have used prepare statemnet*
9.    <% st = con.prepareStatement("update std set ca='"+classesattended+"'  where year=1");
10.     st.executeUpdate();
11.     }%> 

Can i get any help?????

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.