View Full Version : storing in an array
deletedUser2352352
November 21st, 2006, 06:01 AM
Hello all
I have a question.
If i'm pulling data from a database(which i can do); How can i put that data into an array for later use? But i need to be able to use that data more then once on the page.
Every time i try and use the data it only allows me to use it one. Also would it be a multi dimensional array( i think thats what its called.)?
Cheers
noTime
November 21st, 2006, 08:33 AM
Data in your database might change, so I wouldn't chase a solution that makes your array usable again and again (dang, I don't even know such a method :P). If you are curiuos, why don't you use Ajax? :)
Multidimensional array is an array that contains even more arrays.
deletedUser2352352
November 21st, 2006, 10:12 AM
its only so i can show information from the table within database more then once on the page so i can create reports.
So one table may show all the items from that database.
Another table may just show how many item are within that table. add edit them all up.
Its just so i don't have to keep querying the database or is that how you do it anyway?
noTime
November 21st, 2006, 10:29 AM
Um, what's the problem then?
<?php
echo "Paint your table..."
?>
Some HTML stuff.<br />
<?php
echo "Continue your play with arrays."
?>
deletedUser2352352
November 21st, 2006, 10:38 AM
my problem is once i use the information from the array i can't use it again
this is the code i'm using
$result = $connector->query('SELECT ID,user,thegroup,firstname,surname,enabled,created ,logged_in,log_time,last_log FROM cmsusers ORDER BY ID');
$userTrack = array();
while ($row = $connector->fetchArray($result)){
echo"".$row['ID']." | ".$row['user']." | ".$row['thegroup']." | ".$row['firstname']." | ".$row['surname']." | ".$row['enabled']." | ".$row['created']." | ".$row['logged_in']." | ".$row['log_time']." | ".$row['last_log']."<br />";
}
Once that data has been displayed i can't use it again
CrayonViolent
November 21st, 2006, 11:56 AM
$result = $connector->query('SELECT ID,user,thegroup,firstname,surname,enabled,created ,logged_in,log_time,last_log FROM cmsusers ORDER BY ID');
// instead of echoing out stuff in the while loop, store it in an array called $usertrack
// the [] tells php to use the next available array position on each pass of the loop
// so it starts at 0 and makes an array called $usertrack of x elements where x is
// however many rows you have.
while ($row = $connector->fetchArray($result)){
$usertrack[] = $row;
}
// examples (remember your array starts at 0 not 1
// this is what is meant by having a multi-dimensional array. For each element
// of $usertrack, there is an array of information like 'user','firstname','id',etc...
echo $usertrack[0]['user']; // echo the user from the first row
echo $usertrack[3]['firstname']; // echo firstname from the 4th row
// example of dumping it all out at once.
// note that if you used the fetch_array, unless you flagged it to return
// only one array type (MYSQL_ASSOC or MYSQL_NUM)
// this will echo out 'double' info, because fetch_array returns both an associative
// and a numerical version of your info.
foreach($usertrack as $primeKey => $primeVal) {
foreach($primeVal as $key => $val) {
echo "row: $primeKey field: $key data: $val </br>";
}
}
deletedUser2352352
November 21st, 2006, 12:05 PM
bash
thats what i wanted thanks a lot.
Genius i couldn't get my head round it see.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.