ricky spires Posted October 11, 2011 Share Posted October 11, 2011 hello im having a little trouble getting what i want back from my loop. on my page i have 2 placeholders. each placeholder needs to pull the element id from the db that belong in that placeholder so in the db i have these columns: id = (the placeholder id) number = (the placeholder number) element. = (the element id) for example: place holder 1 has element_id 1 & 2 place holder 2 has element_id 1 & 3 id - number - element_id 1 - 1 - 1 2 - 1 - 2 3 - 2 - 1 4 - 2 - 3 in the places holder if have: //place holder 1 $phNo = "1"; $holder = Placeholders::find_by_phID($phNo); //place holder 2 $phNo = "2"; $holder = Placeholders::find_by_phID($phNo); this is the class code: - I MIGHT BE DOING THIS WRONG public function find_by_phID($phNo=0){ $sql = "SELECT * FROM ".self::$table_name." WHERE number=".$phNo.""; $result_array = self::find_by_sql($sql); return !empty($result_array) ? array_shift($result_array) : false; } THIS IS WHAT I TRIED: inside the placeholder i put a loop: <?php $phNo = "1"; $holder = Placeholders::find_by_phID($phNo); foreach ($holder as $holders){ echo "Elements id = ".$holder->contElements_id."<br />"; } echo "<br/>"; ?> but the retults are all wrong. it comes back with: Elements id = 5 Elements id = 5 Elements id = 5 Elements id = 5 Elements id = 5 Elements id = 5 Elements id = 5 what placeholder 1 should come back with is just element id 1 & 2 Elements id = 1 Elements id = 2 any thoughts thanks rick Quote Link to comment https://forums.phpfreaks.com/topic/248934-loop-question/ Share on other sites More sharing options...
requinix Posted October 11, 2011 Share Posted October 11, 2011 return !empty($result_array) ? array_shift($result_array) : false; That line returns the first row in the resultset. You don't want the first row - you want everything. Just return $result_array; Quote Link to comment https://forums.phpfreaks.com/topic/248934-loop-question/#findComment-1278417 Share on other sites More sharing options...
ricky spires Posted October 12, 2011 Author Share Posted October 12, 2011 THANKS that did the trick Quote Link to comment https://forums.phpfreaks.com/topic/248934-loop-question/#findComment-1278563 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.