Forum Moderators: coopster

Message Too Old, No Replies

Array and SQL queries

How to output an array?

         

tomda

11:10 am on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am sure it is a dumb question.

I have stupid table like this one:
ID ¦ PERCENTAGE
1¦20%
2¦50%
3¦30%
4¦15%

Now, the stupid query, get the pecentage for an ID between 2 and 4.

Could you show me how to get the following array as an output:

$stupid_array = array('50%', '30%','15%');

So that I can simply remove duplicate and do an action for each element of the array (create horizontal bars in CSS for my graph).

Thank you

tomda

11:27 am on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the way I am doing it.


/* Create empty array */
$fArray = array();

$p= "select id,percentage from table where id between '2' AND '5'";
$res=mysql_query($p) or die ("Fail pde");
while ($pres=mysql_fetch_array($res)) {
$id=$pres["id"]; $percentage=$pres["percentage"];

/* Add elements to array */
array_push($fArray, $pres["percentage"]); }

/* List each element */
while (list($key,$value) = each($fArray)) {
echo "$key : $value<br>";
}

RonPK

1:52 pm on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, array_push() should work fine. More or less similar is this notation:

$fArray[] = $pres["percentage"]);