florenceale
florenceale

Reputation: 7

Looping an array with php

I have a code like this

$events = array(


array(
    "id" => 101,
    "start_date" => "2016-04-05 08:00:00",
    "end_date" => "2016-04-09 09:00:00",
    "text" => "text1",
    "rec_type" => "week_2___3,5",
    "event_pid" => null,
    "event_length" => 3600
),

array(
    "id" => 102,
    "start_date" => "2016-04-06 12:00:00",
    "end_date" => "2016-04-06 18:00:00",
    "text" => "text2",
    "rec_type" => "",
    "event_pid" => null,
    "event_length" => null
),

array(
    "id" => 103,
    "start_date" => "2016-04-07 12:00:00",
    "end_date" => "2016-04-07 18:00:00",
    "text" => "text3",
    "rec_type" => "",
    "event_pid" => null,
    "event_length" => null
),

array(
    "id" => 104,
    "start_date" => "2016-04-08 12:00:00",
    "end_date" => "2016-04-08 18:00:00",
    "text" => "text4",
    "rec_type" => "",
    "event_pid" => null,
    "event_length" => null
)
);

Now I should get some data from MySQL and make this dynamically but maintaining the variable $events the same as there.

I have tried in some way with while and for, but maybe I made some mistake in the syntax.

Upvotes: -1

Views: 74

Answers (3)

florenceale
florenceale

Reputation: 7

I think it has to be something like this, but it's not working yet...

$q_events = dbquery("select from * calendars where id_event = 1");


$events = array();
$i=0;

while ($q_ev=mysql_fetch_array($q_events)) {
            $events[$i]['id'] = $q_ev['event_id'];
            $events[$i]['start_date'] = $q_ev['start_date'];
            $events[$i]['end_date'] = $q_ev['end_date'];
            $events[$i]['text'] = $q_ev['text'];
            $events[$i]['rec_type'] = $q_ev['rec_type'];
            $events[$i]['event_pid'] = $q_ev['event_pid'];
            $events[$i]['event_length'] = $q_ev['event_length'];            
    $i++;   
        }


$title = $_GET['idstruttura']."_".changeLetters($_GET['nomestruttura']);
require_once("../../ical/codebase/class.php");
$export = new ICalExporter();
$export->setTitle($title);
$ical = $export->toICal($events);

if I do print_r on the array after the query the result is like this

Array ( [0] => Array ( [id] => 413 [start_date] => 2016-02-15 00:00:00 [end_date] => 2016-02-21 00:00:00 [text] => fafafa [rec_type] => week_2___3,5 [event_pid] => 213 [event_length] => 518400 ) [1] => Array ( [id] => 408 [start_date] => 2016-01-11 00:00:00 [end_date] => 2016-01-18 00:00:00 [text] => evento_da_pannello [rec_type] => week_2___3,5 [event_pid] => 213 [event_length] => 604800 ) [2] => Array ( [id] => 410 [start_date] => 2015-12-28 00:00:00 [end_date] => 2015-12-31 00:00:00 [text] => aaa [rec_type] => week_2___3,5 [event_pid] => 213 [event_length] => 259200 ) )

it looks correct, strange

Upvotes: 0

Luke Joshua Park
Luke Joshua Park

Reputation: 9806

To loop through an array within an array?

foreach($arr1 as $arr2) {
    foreach($arr2 as $val) {
        // Your code
    }
}

Upvotes: 2

thepiyush13
thepiyush13

Reputation: 1331

Try this :

$events = //your array 
$sth = $dbh->query('SELECT * FROM tbl');

while ($row = $sth->fetchRow()) {
   array_push($events,$row)
}

Upvotes: 2

Related Questions