Page 1 of 1

creating an array from sql content

Posted: Mon Sep 22, 2008 5:30 am
by Blondy
hi guys this is a sample from the array I need

Code: Select all

$classes_arr = array(
    // Classes for monday (day 1)
    0 => array(
            // Adds a class at 12pm (1200 hours)
            1200 => array(
                "html" => "<b>Psychology</b><br>Room 404", // Display 'Phsychology: Room 404'
                "style" => "background-color: #66CCCC", // use style property to change the background color
                "interval" => 120 // set the interval for 2hrs
            )
    ),
 
    // Classes for wednesday (day 3)
    3 => array(
            // Adds a class at 11am (1100 hours)
            1100 => array(
                "html" => "<b>English 101</b><br>Room 235", // Display 'English 101: Room 235'
                 "interval" => 120 // set the interval for 2hrs
            )
    )
);
I need to add each class to this array from mysql but I cant use while inside the array to add each class
I don't know alot about array but if there be a command like $array="".$array."newwarray" or something like it or any other way plz help me
(I mean I need to create such an array from sql to draw my classes)
thanks

Re: creating an array from sql content

Posted: Mon Sep 22, 2008 2:46 pm
by califdon
It's difficult for me to understand what you are trying to do. It might help if you could show us the SQL statement or the table structure from which your data will come.

Re: creating an array from sql content

Posted: Mon Sep 22, 2008 3:45 pm
by Blondy
I have the data for a class in below table

Code: Select all

CREATE TABLE schedule(
program TEXT NOT NULL,
duration TEXT NOT NULL,
day TEXT NOT NULL,
clock TEXT NOT NULL
);
then I need the array to darw

Code: Select all

$classes_arr = array(
    // Classes for monday (day 1)
    $r[day] => array(
            $r[clock] => array(
                "html" => ".$r[program].", 
                "style" => "background-color: #66CCCC", // use style property to change the background color
                "interval" => $r[duration] // set the interval for 2hrs
            )
    ),
);
I need the to have an array with all of classes but I can't use while inside the array it gives me error
I mean for second class I shuld add another array like first post but how I can't realize with my current knowledge
plz guide me

Re: creating an array from sql content

Posted: Mon Sep 22, 2008 5:34 pm
by califdon
Well, it all depends on what you want to use the array for. My first guess is that you want a multidimensional array that has an element for each row in the table, containing the day, clock, program and duration values. You would need to use a while loop to read each table row and add a new element in your array. I will have to guess at how you want it to work, but maybe something like this:

Code: Select all

...
$classes_arr = array();
while ($row=mysql_fetch_assoc($result)) {
    extract($row);
    $classes_arr = array(
        $day => array(
            'clock' => '$clock',
            'html' => '$program',
            'style' => "background-color: #66CCCC",
            'interval' => '$duration') );
}
That may not be entirely correct; I have some problems with multidimensional arrays, myself. I usually have to try out something and fiddle with it until it works.

Re: creating an array from sql content

Posted: Mon Sep 29, 2008 2:33 pm
by Blondy
Fine
But Could you plz give me more try this One is not working :cry:

Re: creating an array from sql content

Posted: Mon Sep 29, 2008 7:18 pm
by califdon
Blondy wrote:Fine
But Could you plz give me more try this One is not working :cry:
As I said, I usually have to spend some time fiddling with multidimensional arrays to get them right. I can afford to do that on my own projects, where I understand clearly what I'm trying to accomplish. I could never do that on somebody else's project.

Re: creating an array from sql content

Posted: Tue Sep 30, 2008 5:47 am
by Blondy
so plz give me some links for understanding this kind of arrays
thanks :drunk:

Re: creating an array from sql content

Posted: Tue Sep 30, 2008 5:57 am
by papa