thomasw_lrd Posted February 10, 2012 Share Posted February 10, 2012 I've got a problem I can't figure out and I could use some help. The array $x works fine inside the for loop, when I try to access it outside the for loop, It only contains the first record. myload is a custom function written by my predecessor to load up mysql records into an array. $rdrs = myload($sql); $count = count($rdrs); if (count($rdrs)>0){ for($i=0; $i<$count; $i++) { $x=$rdrs[$i]; //This works displays all records //echo "<pre>"; print_r($x); echo "</pre>"; } } //This doesnt, displays the same record. echo "<pre>"; print_r($x); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/ Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 Here is my output. nside for loop Array ( [0] => Jul-2016 [pay_date310511] => Jul-2016 [1] => 2012-01-12 [pay_date_payment_recieved310511] => 2012-01-12 [2] => 900.00 [pay_amount_recieved310511] => 900.00 [3] => No [pay_default310511] => No ) Inside for loop Array ( [0] => Aug-2016 [pay_date310511] => Aug-2016 [1] => [pay_date_payment_recieved310511] => [2] => [pay_amount_recieved310511] => [3] => [pay_default310511] => ) Outside for loop Array ( [0] => Aug-2016 [pay_date310511] => Aug-2016 [1] => [pay_date_payment_recieved310511] => [2] => [pay_amount_recieved310511] => [3] => [pay_default310511] => ) Inside for loop Array ( [0] => Jul-2016 [pay_date310512] => Jul-2016 [1] => 2012-01-12 [pay_date_payment_recieved310512] => 2012-01-12 [2] => 900.00 [pay_amount_recieved310512] => 900.00 [3] => No [pay_default310512] => No ) Inside for loop Array ( [0] => Aug-2016 [pay_date310512] => Aug-2016 [1] => [pay_date_payment_recieved310512] => [2] => [pay_amount_recieved310512] => [3] => [pay_default310512] => ) Outside for loop Array ( [0] => Aug-2016 [pay_date310512] => Aug-2016 [1] => [pay_date_payment_recieved310512] => [2] => [pay_amount_recieved310512] => [3] => [pay_default310512] => Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316611 Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 So I've narrowed it down to the array inside the for loop. I need to make an array of all records, but this is not what is happening. It's overwriting the array, which is why I'm getting Aug 2016 outside the loop. I can't seem to get the concat to work though. $x.=$rdrs[$i]; It sorta works, but it adds the same record multiple times. Suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316615 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 rdrs is already an array of all the records, what are you even trying to accomplish with the $x variable? Also, to append to an array you use $arrayName[] = $whatever. Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316637 Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 here's the whole function as it stands now. And I have attached the output as file. function payments_section2($rec_id) { global $edit, $styles, $p; $ret=''; $sql = "SELECT tr_policy_number from transactions where tr_id = '$rec_id';"; $rs = myload($sql); $policy_number = $rs[0]['tr_policy_number']; $sql = "SELECT id, pay_date, pay_date_payment_recieved, pay_amount_recieved, pay_default FROM transactions_payments where pay_policy_number = '$policy_number' order by id;"; //echo $sql; echo "<br>"; $rs = myload($sql); //echo "<pre>"; print_r($rs); echo "</pre>"; $ret.='<table class="listgrid"> <tr class="listrow"> <th class="listgrid">Monthly Payment</th> <th class="listgrid">Date Payment Recieved</th> <th class="listgrid">Amount Received</th> <th class="listgrid">Defaulted</th> </tr>'; if (count($rs)>0){ foreach ($rs as $r) { //echo "<pre>"; print_r($r); echo "</pre>"; $sql = "SELECT pay_date as pay_date".$r['id'].", pay_date_payment_recieved as pay_date_payment_recieved".$r['id'].", pay_amount_recieved as pay_amount_recieved".$r['id'].", pay_default as pay_default".$r['id']." FROM transactions_payments where pay_policy_number = '$policy_number' order by id;"; //echo $sql; echo "<br>"; $rdrs = myload($sql); $count = count($rdrs); if (count($rdrs)>0){ $ret.= '<tr class="listrow"> <td class="listgrid">'.$r['pay_date'].'</td> <td class="listgrid">'.display_field('', $rdrs[0],'pay_date_payment_recieved'.$r['id'], $styles['osreqdate']).'</td> <td class="listgrid">'.display_field('', $rdrs[0],'pay_amount_recieved'.$r['id'], $styles['osreqstring']).'</td> <td class="listgrid">'.display_field('', $rdrs[0],'pay_default'.$r['id'], $styles['osreqstring']).'</td> </tr>'; } //} } $ret.='</table>'; } return $ret; } Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316648 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 That's lovely. There was a question back there. Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316652 Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 That's lovely. There was a question back there. I'm trying to reuse some code we had written to create an outstanding requirements table. The $x was used to update our secondary table. Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316654 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 Ok, one more time: If you're trying to make $x into an array of all the $rdrs records, $rdrs is already that array, why are you trying to make two copies of it? Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316659 Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 I have no good reason for it. None at all. Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316661 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 Ok...what's the problem then? You originally came on here to post a completely unnecessary loop. Remove the loop, is the problem still a problem? Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316682 Share on other sites More sharing options...
digibucc Posted February 10, 2012 Share Posted February 10, 2012 as Dan said, $rdrs = myload($sql); creates an array of all the records, namely the $rdrs array. $x was never an array, it was a single variable being overwritten in a loop for no reason. so , $rdrs is your array - what do you need to do with it? edit: ok - your problem is in your return, lines 40-45 of your function <?php $ret.= '<tr class="listrow"> <td class="listgrid">'.$r['pay_date'].'</td> <td class="listgrid">'.display_field('', $rdrs[0],'pay_date_payment_recieved'.$r['id'], $styles['osreqdate']).'</td> <td class="listgrid">'.display_field('', $rdrs[0],'pay_amount_recieved'.$r['id'], $styles['osreqstring']).'</td> <td class="listgrid">'.display_field('', $rdrs[0],'pay_default'.$r['id'], $styles['osreqstring']).'</td> </tr>'; ?> <?php $rdrs[0] ?> is selecting the same record every time. based on the declaration: <?php function payments_section2($rec_id) { ?> i think those line should read <?php $ret.= '<tr class="listrow"> <td class="listgrid">'.$r['pay_date'].'</td> <td class="listgrid">'.display_field('', $rdrs[$rec_id],'pay_date_payment_recieved'.$r['id'], $styles['osreqdate']).'</td> <td class="listgrid">'.display_field('', $rdrs[$rec_id],'pay_amount_recieved'.$r['id'], $styles['osreqstring']).'</td> <td class="listgrid">'.display_field('', $rdrs[$rec_id],'pay_default'.$r['id'], $styles['osreqstring']).'</td> </tr>'; ?> i'd also recommend not having so much html in your function, it should return values and that's it. Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316694 Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 Thank you. I agree with the HTML statement, but I inherited this system from someone who is much more skilled than I am. It would take too much time to rewrite his code to match how I would do it. The problem with $rec_id is that is for our transactions table, it has nothing to do with this function. I'm trying to create a way to update a new table, transactions_payments. (I know it doesn't make any sense, my former boss was a businessman, who had a little coding and wanted it like this). Each transaction has a start date and an end date and we have to track the payments for every month. You have pointed me in the right direction. I was using the $x variable to display each record as an individual. Elsewhere in my code is this statement elseif (substr($k, 0, 4)=='pay_') { preg_match('/pay_date(\d+)/', $k, $rdm); if (count($rdm)>0){ $pay_date=''; if (isset($_POST['pay_date'.$rdm[1]])) $pay_date=$_POST['pay_date'.$rdm[1]]; echo $pay_date; echo "<br>"; $sql = "UPDATE transactions_payments set pay_date = '$pay_date' where id = '".$rdm[1]."'"; echo $sql; echo "<br>"; $update_payment_table = myexecute($sql); } } } This does the update correctly. But then the display code does not work. I had the display code working yesterday for one field, until I did the update code. Then the display code stopped working. Now I'm not sure how to get the update code to work. I think I can make it work with a bastardized (I hope that's okay to post, if not I'm sorry I will remove it) update button. But I didn't want to go that route. I actually have a version with two pages, one to do the display, and one to do the update. I'm just trying to make it all work on one page. I was hoping there was a "magic" answer, something stupid I had missed. Which there are several stupid things I have missed as has been posted above. Thanks for all the help so far. Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316720 Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 I've narrowed it down a little further. And just had an insight while typing this. My problem is, I'm selecting multiple records with the policy number. That is where I messed up. My underlying table structure is wrong. That is why it doesn't work. Thanks for all your help. I wouldn't have noticed this without thinking throught what both of you had told me. Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316731 Share on other sites More sharing options...
thomasw_lrd Posted February 10, 2012 Author Share Posted February 10, 2012 Just to be complete. Here is my code to dsiplay everything properly. function payments_section2($rec_id) { global $edit, $styles, $p; $ret=''; $sql = "SELECT tr_policy_number from transactions where tr_id = '$rec_id';"; $rs = myload($sql); $policy_number = $rs[0]['tr_policy_number']; $sql = "SELECT id, pay_date, pay_date_payment_recieved, pay_amount_recieved, pay_default FROM transactions_payments where pay_policy_number = '$policy_number' order by id;"; $rs = myload($sql); $ret.='<table class="listgrid"> <tr class="listrow"> <th class="listgrid">Monthly Payment</th> <th class="listgrid">Date Payment Recieved</th> <th class="listgrid">Amount Received</th> <th class="listgrid">Defaulted</th> </tr>'; if (count($rs)>0) { foreach ($rs as $r) { $sql = "SELECT pay_date as pay_date".$r['id'].", pay_date_payment_recieved as pay_date_payment_recieved".$r['id'].", pay_amount_recieved as pay_amount_recieved".$r['id'].", pay_default as pay_default".$r['id']." FROM transactions_payments where pay_policy_number = '$policy_number' and id = ".$r['id']." order by id;"; $rdrs = myload($sql); if (count($rdrs)>0){ if (count($rdrs)>0){ $x=$rdrs[0]; } $ret.= '<tr class="listrow"> <td class="listgrid">'.$r['pay_date'].'</td> <td class="listgrid">'.display_field('', $x,'pay_date_payment_recieved'.$r['id'], $styles['osreqdate']).'</td> <td class="listgrid">'.display_field('', $x,'pay_amount_recieved'.$r['id'], $styles['osreqstring']).'</td> <td class="listgrid">'.display_field('', $x,'pay_default'.$r['id'], $styles['osreqstring']).'</td> </tr>'; } //echo "rid:".$r['id'].":dir"; } $ret.='</table>'; } return $ret; } And here is the code that actually updates the records. if ($faction=='update' && $update_permission){ $sql="SELECT COUNT(".$table['key'].") FROM ".$table['name']." WHERE ".$table['key']."=".cleansql($rec_id); $rs=myload($sql); if ($rs[0][0]=='1'){ $vars=''; foreach ($_POST as $k=>$v){ if (isset($tablestructure[$k]) && stristr($tablestructure[$k]['Extra'], 'auto_increment')===false){ // edit for required non-null if ($tablestructure[$k]['Null']=='NO' && $v=='') $fieldeditproblems.=' - '.$k." IS EMPTY. IT CANNOT BE EMPTY.\n"; // edit for datetime fields if (($tablestructure[$k]['Type']=='datetime' || $tablestructure[$k]['Type']=='timestamp') && ($v<>'' && !is_date($v))) $fieldeditproblems.=' - '.$k." DOES NOT APPEAR TO BE A VALID DATE. IT MUST BE A VALID DATE.\n"; // edit for truncated varchars if (preg_match('/(?P<type>\w+)($|\((?P<length>(\d+|(.*)))\))/', $tablestructure[$k]['Type'], $matches) && $matches[1]=='varchar' && $matches['length']<strlen($v)) $fieldeditproblems.=' - '.$k." WILL TRUNCATE DATA. IT ONLY ALLOWS ".$matches['length']." CHARS, YET YOU HAVE ENTERED ".strlen($v).".\n"; $vars.=','.$k.'='.cleansql($v); } elseif (substr($k, 0, 4)=='pay_') { preg_match('/pay_date_payment_recieved(\d+)/', $k, $rdm); if (count($rdm)>0){ $pay_date=''; if (isset($_POST['pay_date'.$rdm[1]])) $pay_date=$_POST['pay_date'.$rdm[1]]; $pay_date_payment_recieved=''; if (isset($_POST['pay_date_payment_recieved'.$rdm[1]])) $pay_date_payment_recieved=$_POST['pay_date_payment_recieved'.$rdm[1]]; $pay_amount_recieved=''; if (isset($_POST['pay_amount_recieved'.$rdm[1]])) $pay_amount_recieved=$_POST['pay_amount_recieved'.$rdm[1]]; $pay_default=''; if (isset($_POST['pay_default'.$rdm[1]])) $pay_default=$_POST['pay_default'.$rdm[1]]; $sql = "UPDATE transactions_payments set pay_date_payment_recieved = '$pay_date_payment_recieved', pay_amount_recieved = '$pay_amount_recieved', pay_default = '$pay_default' where id = '".$rdm[1]."'"; $update_payment_table = myexecute($sql); } } } if ($fieldeditproblems==''){ $vars=substr($vars,1); $sql="UPDATE ".$table['name']." SET ".$vars." WHERE ".$table['key']."=".$rec_id; $updrs=myexecute($sql); $updatestatus='Record saved. '; } else { // we encountered field edit issues, don't even try insert $updatestatus="Record update not performed. The following field validation errors were found:<br><pre>".$fieldeditproblems."</pre>"; } } else { $updatestatus='Record NOT saved, no matching record was found. '; } } Quote Link to comment https://forums.phpfreaks.com/topic/256824-array-help/#findComment-1316765 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.