Jump to content


Photo

**SOLVED**Fill an array


  • Please log in to reply
6 replies to this topic

#1 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 11 October 2006 - 08:15 PM

I have a table and I want to put a couple fields into an array. I would like to query the table with my parameters then fill the array the keys will be one field and the values will be another field. I tried this but it is not correct

<?php
$accounts = array();
$sql = "SELECT * FROM tokayacct";
  $res = mysql_query($sql) or die (mysql_error());
    while($r=mysql_fetch_assoc($res)){
$accounts[0] = $r['account_num'];
$accounts[1] = $r['meter_num'];
}
?>

thanks guys

Ray

#2 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 11 October 2006 - 08:22 PM

ok so account_num , meter_num , are in your table. You want this stored in an array.. maybe store it by id and put another array in there.

you could use something like:
<?php

$accounts = array();
$sql = "SELECT * FROM tokayacct";
  $res = mysql_query($sql) or die (mysql_error());
    while($r=mysql_fetch_assoc($res)){
    $accounts[$r[id]] = array($r['account_num'], $r['meter_num']);
}

?>

something like that might work..

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 October 2006 - 08:26 PM

The reason the OP's original code does not work is that the OP is constantly overlaying the first and second elements in the array.

Another way of doing this would be:
<?php
$accounts = array();
$sql = "SELECT * FROM tokayacct";
  $res = mysql_query($sql) or die (mysql_error());
    while($r=mysql_fetch_assoc($res)){
    $accounts[$r['account_num']] = $r['meter_num'];
}?>

Which builds an array that is keyed by the account_number.

Ken

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 11 October 2006 - 08:28 PM

<?php
Maybe like...

<?

$accounts = array ();

$sql = "SELECT * FROM tokayacct";

$res = mysql_query ( $sql ) or die ( mysql_error () );

while ( $r = mysql_fetch_assoc ( $res ) )
{
	$accounts[$r['account_num']] = $r['meter_num'];
}

?>


me!

#5 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 11 October 2006 - 08:45 PM

Make a 2 dimensional array:
<?php
$accounts = array();
$currow = 0; //initialize
$sql = "SELECT * FROM tokayacct";
  $res = mysql_query($sql) or die (mysql_error());
    while($r=mysql_fetch_assoc($res)){
        //2 dimensional array notation used below
        $accounts[$currow]['account_num'] = $r['account_num'];
        $accounts[$currow]['meter_num'] = $r['meter_num'];
        //add other fields you want to store here
        $currow++; //increment first array index
    }
?>


#6 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 11 October 2006 - 08:47 PM

--

#7 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 12 October 2006 - 07:46 PM

Thanks for the replies fellas. Kens worked great.

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users