creating an array from sql content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Blondy
Forum Commoner
Posts: 32
Joined: Thu Mar 06, 2008 5:55 pm

creating an array from sql content

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: creating an array from sql content

Post 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.
Blondy
Forum Commoner
Posts: 32
Joined: Thu Mar 06, 2008 5:55 pm

Re: creating an array from sql content

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: creating an array from sql content

Post 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.
Blondy
Forum Commoner
Posts: 32
Joined: Thu Mar 06, 2008 5:55 pm

Re: creating an array from sql content

Post by Blondy »

Fine
But Could you plz give me more try this One is not working :cry:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: creating an array from sql content

Post 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.
Blondy
Forum Commoner
Posts: 32
Joined: Thu Mar 06, 2008 5:55 pm

Re: creating an array from sql content

Post by Blondy »

so plz give me some links for understanding this kind of arrays
thanks :drunk:
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: creating an array from sql content

Post by papa »

Post Reply