Jump to content

Loop not continuing from function


kts

Recommended Posts

(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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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