Jump to content

Query into array


goosebriar

Recommended Posts

I am trying to create a new array out of results from a query, but can't get it to create from more than one row of the results.  How can I get the rest of the rows?

 

$entry=sprintf("SELECT * FROM lrms_entry 
		WHERE start >='$start'
		AND start <'$end'");
		$rs_entry=mysql_query($entry);
		$row_rs_entry=mysql_fetch_assoc($rs_entry);

do {
	$period=30;
           $starttime=$row_rs_entry['start'];
	$endtime=$row_rs_entry['end'];
	$diff=$endtime-$starttime;
	$periodno=($diff/$period)/60;
	for ($c=1; $c<$periodno; $c++) {
		$add=($c*$period)*60;
		$middle[$c]['time']=$start+$add;
		$middle[$c]['type']="middle";
	}
} while ($row_rs_entry=mysql_fetch_assoc($rs_entry));

 

This only makes an array of the first record.  How do I get the rest?

Link to comment
https://forums.phpfreaks.com/topic/58261-query-into-array/
Share on other sites

Wow - that was fast.

 

Sometimes, I will have no results or I may several results.

 

I dropped the first result and did a while loop, but it still only creates an array from one record. 

 

$entry=sprintf("SELECT * FROM lrms_entry 
		WHERE start >='$start'
		AND start <'$end'");
		$rs_entry=mysql_query($entry);
		$numrecords=mysql_num_rows($rs_entry);
		echo "Num records: ".$numrecords."<br />";

while ($row_rs_entry=mysql_fetch_assoc($rs_entry)) {
	$starttime=$row_rs_entry['start'];
	$endtime=$row_rs_entry['end'];
	$diff=$endtime-$starttime;
	$periodno=($diff/$period)/60;
	for ($c=1; $c<$periodno; $c++) {
		$add=($c*$period)*60;
		$middle[$c]['time']=$start+$add;
		$middle[$c]['type']="middle";
	}
} 

Link to comment
https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-288846
Share on other sites

Try something like

 

<?php
$data = array();
while ($row_rs_entry=mysql_fetch_assoc($rs_entry)) {
	$starttime=$row_rs_entry['start'];
	$endtime=$row_rs_entry['end'];
	$diff=$endtime-$starttime;
	$periodno=($diff/$period)/60;
                $middle=array();
	for ($c=1; $c<$periodno; $c++) {
		$add=($c*$period)*60;
		$middle[$c]['time']=$start+$add;
		$middle[$c]['type']="middle";
	}
                $data[] = $middle;
} 

Link to comment
https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-288979
Share on other sites

Here's one I created earlier

 

function array_combine ($keys, $data)
    {
        $res = array();
        foreach ($keys as $i=>$k) {
            $res[$k] = $data[$i];
        }
        return $res;
    }

 

How do you want the array to look?

Link to comment
https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-289087
Share on other sites

I would like it to look like:

 

[6] => Array

        (

            [time] => 1183473000

            [type] => middle

            [name] =>

            [date] => 10:30

            [bgcolor] =>

 

      )

Right now it looks like:

 

[0] => Array

        (

            [1] => Array

                (

                    [time] => 1183473000

                    [type] => middle

                    [name] =>

                    [date] => 10:30

                    [bgcolor] => 1

                )

 

I just need to  get rid of that extra level.  Right now I'm experimenting with some looping to make a new array, but its not happening.

 

I tried your example, but got "Warning: Invalid argument supplied for foreach() in".

 

Thanks for all your help, btw!

Link to comment
https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-289097
Share on other sites

The only way I can see of reducing the levels is if $c keeps accumulating from record to record

 

$middle = array();
$n = 1;
while ($row_rs_entry=mysql_fetch_assoc($rs_entry)) {
	$starttime=$row_rs_entry['start'];
	$endtime=$row_rs_entry['end'];
	$diff=$endtime-$starttime;
	$periodno=($diff/$period)/60;
                
	for ($c=$n; $c<= $n + $periodno; $c++) {
		$add=($c*$period)*60;
		$middle[$c]['time']=$start+$add;
		$middle[$c]['type']="middle";
	}
                $n=$c;
} 

Link to comment
https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-289153
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.