Jump to content

Help on returning array values


Go to solution Solved by neilfurry,

Recommended Posts

Hello Mate,

 

I have another problem, though i already fixed the problem on my previous post, i came across on another problem.

 

i have this code:

$sql = mysql_query("SELECT * FROM schedules") or die(mysql_error());

$results = array();
    while($rs=mysql_fetch_array($sql)){
                $results['events'] = array([
                    
                    'id'    =>    (int)$rs['id'],
                    'start'    =>    $rs['date_start'],
                    'end'    =>    $rs['date_end'],
                    'title'    =>    $rs['details'],
                    'free' =>    (bool)$rs['isfree'],
                    'userId' => (int)$rs['techID'],
                    'color'    => render_color_code($rs['zip'])
                ]);
    }

 

I have  many events on my database, but only one is being displayed using the script above. how can i make it so that this script will get all events from my database. can you check what is wrong with my coding?

 

Cheers!

Neil

 

Link to comment
https://forums.phpfreaks.com/topic/300002-help-on-returning-array-values/
Share on other sites

You're overwriting $results['events'] every time. What you should do is start with "events" as an array and then add to it.

 

$sql = mysql_query("SELECT * FROM schedules") or die(mysql_error());
$results = array('events' => array());
    while($rs=mysql_fetch_array($sql)){
                $results['events'][] = array(
                    
                    'id'    =>    (int)$rs['id'],
                    'start'    =>    $rs['date_start'],
                    'end'    =>    $rs['date_end'],
                    'title'    =>    $rs['details'],
                    'free' =>    (bool)$rs['isfree'],
                    'userId' => (int)$rs['techID'],
                    'color'    => render_color_code($rs['zip'])
                );
    }
And you're nesting your arrays wrong. array([]) will put an array inside another array. Use either the array() or [] syntax to get just the one array.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.