-1

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.

3
  • 1
    You have to be a bit more specific. What exactly is the relation between this array and your SQL query? Commented Dec 3, 2015 at 20:43
  • The data from mysql, do you want to combine it with the above array and use $events to store both arrays ? or you just want codes to iterate over the above array ? Commented Dec 3, 2015 at 20:43
  • @andre3wap I want to combine the data with the array, replacing a static example with a dynamic one. Commented Dec 3, 2015 at 23:30

3 Answers 3

2

Try this :

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

while ($row = $sth->fetchRow()) {
   array_push($events,$row)
}
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think this is what the OP is asking for, but his question is very vague. I was under the impression he wanted to loop through the array he has already.
agree the question is not clear, but looking at the array, it seems like it contains rows and columns and is returned from a database call that`s why I posted a likely answer.anyway thanks for the notice
Ahh I see what you mean. Both are viable I guess. OP needs to be more clear.
no that is just a static example, I think Luke Park is right. That's just the code I had and it is working...but I just don't know how to change it in a dynamic code, generated every time dynamically, and make that $event exactly the same.
2

To loop through an array within an array?

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

Comments

0

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

1 Comment

"but it's not working yet" -- This is Not An Answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.