goosebriar Posted July 3, 2007 Share Posted July 3, 2007 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 More sharing options...
TreeNode Posted July 3, 2007 Share Posted July 3, 2007 Are you always expecting at least one result? Drop the first mysql_fetch_assoc and make your loop a while-loop Link to comment https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-288845 Share on other sites More sharing options...
goosebriar Posted July 3, 2007 Author Share Posted July 3, 2007 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 More sharing options...
Barand Posted July 3, 2007 Share Posted July 3, 2007 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 More sharing options...
goosebriar Posted July 3, 2007 Author Share Posted July 3, 2007 That worked beautifully! I'm merging this into another array that doesn't have that many levels, though. How do I reduce the levels? I tried array_combine, but I don't have PHP 5. Link to comment https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-289077 Share on other sites More sharing options...
Barand Posted July 3, 2007 Share Posted July 3, 2007 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 More sharing options...
goosebriar Posted July 3, 2007 Author Share Posted July 3, 2007 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 More sharing options...
Barand Posted July 3, 2007 Share Posted July 3, 2007 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 More sharing options...
goosebriar Posted July 5, 2007 Author Share Posted July 5, 2007 Thanks, that didn't work though. I think I will post a new topic. Thanks for all your help! Link to comment https://forums.phpfreaks.com/topic/58261-query-into-array/#findComment-290380 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.