Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 17th, 2005, 04:57 PM
Authorized User
 
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ashleek007
Default setting each value from database as a variable

hey,

I have got a query which selects records from a database, i want to store each value the query produces in a separate variable, for example:-

the table has 10 values, (10,20,30,43,52,68,54,21,98,100)

the query selects all these values, i want to set each one to a separate variable, for example '$value1' would be 10 '$value2' would be 20 etc....

any ideas how i would go about this?

cheers..........again!:D

Ash

My new web design domain
www.askmultimedia.co.uk
__________________
My new web design domain
www.askmultimedia.co.uk
 
Old April 17th, 2005, 05:00 PM
Authorized User
 
Join Date: Apr 2005
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Post what you have so far.

Everything is temporary, some things are just more temporary than others... except for death, that seems to be pretty permanent
 
Old April 17th, 2005, 05:52 PM
Authorized User
 
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ashleek007
Default

$username = $_SESSION['username'];
$testID = $HTTP_SERVER_VARS['QUERY_STRING'];
$query1 = "SELECT * FROM Results WHERE Username = '$username' AND TestID = '$testID' ORDER BY ResultsID DESC";
$result1 = mysql_query($query1);
$num = 9;

             while($row1 = mysql_fetch_array($result1,MYSQL_ASSOC))
             {
            $num++;
            $result.$num = $row1[TestScore];
            }

    $values = array("$result10","$result11","$result12","0","47" ,
                    "0","67","77","87","90","100");

hey, thanks for the fast reply, heres my code so far, i know it selects the right data because ive echoed the variable. I jst need these values to be set as indipendant variables so i can use them in the array!

Cheers
Ash

My new web design domain
www.askmultimedia.co.uk
 
Old April 17th, 2005, 06:06 PM
Authorized User
 
Join Date: Apr 2005
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Im' confused at what you want it looks as though you are already doing it. You are sepreateing the in diffrent vars as $result10, $result11, ect. But then are putting them together again in $values = array(...)

Everything is temporary, some things are just more temporary than others... except for death, that seems to be pretty permanent
 
Old April 17th, 2005, 06:09 PM
Authorized User
 
Join Date: Apr 2005
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just change $result.$num = $row1[TestScore] to $value.$num = $row1[TestScore] to get them to be $value10 = 10, $value20 = 20, or if you want $value1 = 10, $value2 = 20 then remove the $num = 9.

Everything is temporary, some things are just more temporary than others... except for death, that seems to be pretty permanent
 
Old April 18th, 2005, 10:33 AM
Authorized User
 
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ashleek007
Default

ok, this isnt working!lol

What i want to do is set the values in the array:-

$values = array("40","30","20","0","47","0","67","77","87"," 90","100");

to the values that are produced from this query:-

$query1 = "SELECT TestScore FROM Results WHERE Username = '$username' AND TestID = '$testID' ORDER BY ResultsID DESC";

so that the values produced from the query(for example 20,32,43) would be set as the array above, like this:-

$values = array("20","32","43");

does that make more sense?
Thanks bobby,
Ash


My new web design domain
www.askmultimedia.co.uk
 
Old April 18th, 2005, 11:36 AM
Authorized User
 
Join Date: Apr 2005
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok got you. Misunderstood before. Just change

 $result.$num = $row1[TestScore];
    ---to---
 $values[] = $row1[TestScore];

it will give you an array just like $values = array("TestScore", "TestScore", "TestScore", "TestScore", "TestScore") untill the end of the while statment. That will also allow you to get rid of the unnesasary
$num = 9;
and
$num++;
 unless you need them elswhere.

    Well that should be what you are looking for. If not just let me know.

Everything is temporary, some things are just more temporary than others... except for death, that seems to be pretty permanent
 
Old April 18th, 2005, 11:43 AM
Authorized User
 
Join Date: Apr 2005
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What took me off track was your first post. It stated " want to store each value the query produces in a separate variable" to me ment 'not an array'.

Also keep in mind that in an array when you are not defining a key that the array starts at 0(zero) not 1(one.) I know that that through me off a bit a couple of times.

Everything is temporary, some things are just more temporary than others... except for death, that seems to be pretty permanent
 
Old April 18th, 2005, 02:45 PM
Authorized User
 
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ashleek007
Default

no mate, this could be the end of me!lol

$username = $_SESSION['username'];
$testID = $HTTP_SERVER_VARS['QUERY_STRING'];
$query1 = "SELECT * FROM Results WHERE Username = '$username' AND TestID = '$testID' ORDER BY ResultsID DESC";
$result1 = mysql_query($query1);

             while($row1 = mysql_fetch_array($result1,MYSQL_ASSOC))
             {
            $values[] = $row1[TestScore];
            }
// Get the total number of columns we are going to plot

    $columns = count($values);

// Get the height and width of the final image

    $width = 300;
    $height = 200;

// Set the amount of space between each column

    $padding = 5;

// Get the width of 1 column

    $column_width = $width / $columns ;

// Generate the image variables

    $im = imagecreate($width,$height);
    $gray = imagecolorallocate ($im,255,000,000);
    $gray_lite = imagecolorallocate ($im,000,000,000);
    $gray_dark = imagecolorallocate ($im,000,000,000);
    $white = imagecolorallocate ($im,255,218,185);


// Fill in the background of the image

    imagefilledrectangle($im,0,0,$width,$height,$white );

    $maxv = 0;

// Calculate the maximum value we are going to plot

    for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv);

// Now plot each column

    for($i=0;$i<$columns;$i++)
    {
        $column_height = ($height / 100) * (( $values[$i] / $maxv) *100);

        $x1 = $i*$column_width;
        $y1 = $height-$column_height;
        $x2 = (($i+1)*$column_width)-$padding;
        $y2 = $height;

        imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray);

// This part is just for 3D effect

        imageline($im,$x1,$y1,$x1,$y2,$gray_lite);
        imageline($im,$x1,$y2,$x2,$y2,$gray_lite);
        imageline($im,$x2,$y1,$x2,$y2,$gray_dark);

    }

// Send the PNG header information. Replace for JPEG or GIF or whatever

    header ("Content-type: image/png");
    imagepng($im);

?>

Theres the whole block of code, the idea is to create a bar graph with the values from the $values array, but i want to set this array from values in my database. do you understand?

I'm trying to plot exam marks, which will change everytime someone takes the online test i have devised, which are stored in a DB.

thanks, so far mate,
Ash



My new web design domain
www.askmultimedia.co.uk
 
Old April 18th, 2005, 02:50 PM
Authorized User
 
Join Date: Apr 2005
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So did $values[] work for you or are you still having the same problem?

Everything is temporary, some things are just more temporary than others... except for death, that seems to be pretty permanent





Similar Threads
Thread Thread Starter Forum Replies Last Post
conditionally setting variable ewel Beginning PHP 2 December 13th, 2007 07:39 PM
problem setting a variable jtrifts Javascript 4 August 9th, 2007 08:36 AM
Newbie : How do you .. without setting a variable rqaran XSLT 2 August 6th, 2005 04:09 AM
declare a variable without setting a value to it crmpicco Javascript How-To 1 July 18th, 2005 07:25 PM
Setting a variable using Select Case Adam ASP.NET 1.0 and 1.1 Basics 2 September 4th, 2004 02:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.