kts Posted December 27, 2007 Share Posted December 27, 2007 (inside functions file) <?php function getinfo_products() { $query = "SELECT * FROM products ORDER BY products_date_added ASC"; $result = mysql_query($query) or die(mysql_error()); for ($i = 0; $i < mysql_num_rows($result); $i++) { $p_array[$i] = mysql_fetch_array($result); } return $p_array; } function getinfo_products_description($p_id) { $description_query = "SELECT * FROM products_description WHERE products_id = $p_id"; $description_result = mysql_query($description_query) or die(mysql_error()); for ($j = 0; $j < mysql_num_rows($description_result); $j++) { $pd_array[$j] = mysql_fetch_array($description_result); } return $pd_array; } ?> the calling <?php $p_Array = getinfo_products(); $aquery = "SELECT * FROM products LIMIT 6"; $aresult = mysql_query($aquery) or die(mysql_error()); for ($n = 0; $n < mysql_num_rows($aresult); $n++) { $p_id = $p_Array[$n]['products_id']; $pd_Array = getinfo_products_description($p_id); ?><td align="center" valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="left" valign="top"><a href="#"><img src="images/<?php echo $p_Array[$n]['products_image']; ?>" alt="" width="125" height="150" /></a></td> </tr> <tr> <td align="left" valign="top" class="price">$<?php echo number_format($p_Array[$n]['products_price'],2); ?></td> </tr> <tr> <td align="left" valign="top"><a href="#"><?php echo $pd_Array[$n]['products_name']; ?></a><br /> <?php echo $pd_Array[$n]['products_description']; ?></td> </tr> </table></td><?php } ?> not sure why it would grab the info properly from the first one, but not continue through the loop.. if anyone can see where i went wrong i'd appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/ Share on other sites More sharing options...
SuperBlue Posted December 27, 2007 Share Posted December 27, 2007 You must type "return $pd_array;" inside the loop, otherwhise it only dose the "return" once... Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/#findComment-424206 Share on other sites More sharing options...
MadTechie Posted December 27, 2007 Share Posted December 27, 2007 without out seeing the whole code i would guess your running both queries on the same connection link.. So.. when the query in the loop is called it replaces the query at the start of the loop.. you could use 2 connections but i think it would be better to use a JOIN.. something like this $query = "SELECT * FROM products LEFT JOIN products_description as PB on PB.ID = products.ID ORDER BY products.products_date_added ASC"; Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/#findComment-424209 Share on other sites More sharing options...
MadTechie Posted December 27, 2007 Share Posted December 27, 2007 You must type "return $pd_array;" inside the loop, otherwhise it only dose the "return" once... He is returning it <?php return $pd_array; } ?> Anywhere else will end the loop! Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/#findComment-424211 Share on other sites More sharing options...
kts Posted December 27, 2007 Author Share Posted December 27, 2007 without out seeing the whole code i would guess your running both queries on the same connection link.. So.. when the query in the loop is called it replaces the query at the start of the loop.. you could use 2 connections but i think it would be better to use a JOIN.. something like this $query = "SELECT * FROM products LEFT JOIN products_description as PB on PB.ID = products.ID ORDER BY products.products_date_added ASC"; yeah, thats along the lines of what i need, im unfamiliar with how to call the two tables with just one query, is there a page to suggest to read on that? i checked the mysql manual but there are so many joins. my tables have products_id in both as primaries I just don't want to have to double function to call it. Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/#findComment-424214 Share on other sites More sharing options...
MadTechie Posted December 27, 2007 Share Posted December 27, 2007 Best place for info is the manual http://dev.mysql.com/doc/refman/5.0/en/join.html BUT heres a few examples http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php i wrote a little test script, to try out <?php function Test() { $query = "SELECT * FROM products_description LEFT JOIN products on products.products_id = products_description.products_id ORDER BY products.products_date_added ASC"; while ($row = mysql_fetch_assoc($result)) { $pd_array = $row; } mysql_free_result($result); return $pd_array; } $tester = Test(); print_r($tester); die; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/#findComment-424223 Share on other sites More sharing options...
kts Posted December 27, 2007 Author Share Posted December 27, 2007 Cool thanks a lot. Just figuring out how I can call them out in echo's and such from the same array thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/#findComment-424235 Share on other sites More sharing options...
kts Posted December 27, 2007 Author Share Posted December 27, 2007 <?php function getinfo_products() { $query = "SELECT products.products_id, products.products_price, products.products_quantity, products.products_price, products.products_status, products_description.products_name, products_description.products_description, products_description.language_id FROM products JOIN products_description ON (products.products_id = products_description.products_id)"; ; $result = mysql_query($query) or die(mysql_error()); for ($i = 0; $i < mysql_num_rows($result); $i++) { $p_array[$i] = mysql_fetch_array($result); } return $p_array; } ?> thats the new function <?php $p_Array = getinfo_products(); $aquery = "SELECT * FROM products LIMIT 6"; $aresult = mysql_query($aquery) or die(mysql_error()); for ($n = 0; $n < mysql_num_rows($aresult); $n++) { ?> <td align="center" valign="top"><?php echo $pd_array[$n]['products.products_id']; ?> thats the new call, im not sure how to call it properly i figured that would be right, but there are no results. Quote Link to comment https://forums.phpfreaks.com/topic/83381-loop-not-continuing-from-function/#findComment-424238 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.