Jump to content

[SOLVED] foreach/while loop = confusion


piznac

Recommended Posts

Ok I have two tables in a rather small database. One holds 3 fields one being a "store number". The other holds info about projects completed for these stores. What I need is to lisst the stores and the projects completed. I can do this,. but not the way I want. I want it to be like this:

Store#1 = project1,project2,project3,..etc.
Store#2 = project1,project2,project3,..etc.

The only thing I can get is:

Store#1 = project1
Store#1 = project2
Store#1 = project3,..etc.
Store#2 = project1,..etc.

I know there must be a way to do this, but am unsure how. This is what I have so far:

[code]<?php require_once('../../Connections/conner.php'); ?>
<?php
$dm = $_GET['dm'];
mysql_select_db($database_conner, $conner);
$query_dmstore = "SELECT * FROM dmrmst";
$dmstore = mysql_query($query_dmstore, $conner) or die(mysql_error());
$row_dmstore = mysql_fetch_assoc($dmstore);
$totalRows_dmstore = mysql_num_rows($dmstore);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>

<body>
<?php do { ?>
  <?php $num = $row_dmstore['store']; ?>
  <?php
$numbs = explode(" ", $num);
foreach($numbs as $value){
mysql_select_db($database_conner, $conner);
$query_store = "SELECT * FROM pog_check WHERE `storenum` = '$value'";
$store = mysql_query($query_store, $conner) or die(mysql_error());
$row_store = mysql_fetch_assoc($store);
$totalRows_store = mysql_num_rows($store);
$pog = $row_store['storenum'];
$pogs = explode(" ", $pog);
foreach ($pogs as $value2){
echo "$value2 = $row_store[pogtype]<br>";
}
}

?>
  <?php } while ($row_dmstore = mysql_fetch_assoc($dmstore)); ?>



</body>
</html>
<?php
mysql_free_result($dmstore);

mysql_free_result($store);
?>[/code]

I know this is probably all wrong,. but I cant seem to wrap my head around this. Any help would be great,.. and if you need more info please tell me.
Link to comment
Share on other sites

maybe this will shed some light on things:
[code]
<?php
$sql1 = "SELECT store_number FROM stores_table";
$query1 = mysql_query($sql1);

while($storenum = mysql_fetch_array($query1)){
$sql2 = "SELECT project FROM projects_table WHERE store_id = ". $storenum['store_number'] ."";
$query2 = mysql_query($sql2);

                $projects_array = array();
while($row = msyql_fetch_array($query2)){
$projects_array[] = $row['project'];
}
$string = explode(", ", $projects_array);

echo "Store#". $storenum['store_number'] ." = ". $string ."<br />\n";
}
?>
[/code]
Link to comment
Share on other sites

Ok sorry now Im even more confused. Now Im getting a inf loop of the first store number. Here's what I have:

[code]<?php require_once('../../Connections/conner.php'); ?>
<?php
$dm = $_GET['dm'];
mysql_select_db($database_conner, $conner);
$query_dmstore = "SELECT * FROM dmrmst";
$dmstore = mysql_query($query_dmstore, $conner) or die(mysql_error());
$row_dmstore = mysql_fetch_assoc($dmstore);
$totalRows_dmstore = mysql_num_rows($dmstore);

while($totalRows_dmstore = mysql_num_rows($dmstore)){
mysql_select_db($database_conner, $conner);
$query_store = "SELECT * FROM pog_check WHERE `storenum` = ". $row_dmstore['store'] ."";
$store = mysql_query($query_store, $conner) or die(mysql_error());
$row_store = mysql_fetch_array($store);
$totalRows_store = mysql_num_rows($store);

while($row_store = mysql_fetch_array($store)){
$projects_array[] = $row['pog_type'];
}
$string = explode(", ", $projects_array);

echo "Store#". $row_dmstore['store'] ." = ". $string ."";
}
?>
[/code]
Link to comment
Share on other sites

that's because you've got it wrong. i've taken a look at your code, and tried to see what it is you want to do... try this:
[code]
<?php
require_once('../../Connections/conner.php');

$dm = $_GET['dm'];
mysql_select_db($database_conner, $conner);

$query_dmstore = "SELECT * FROM dmrmst";
$dmstore = mysql_query($query_dmstore, $conner) or die(mysql_error());

$totalRows_dmstore = mysql_num_rows($dmstore);
        echo "Total number of stores: ". $totalRows_dmstore ."<br />\n";

while($row_dmstore = mysql_fetch_array($dmstore)){
$query_store = "SELECT * FROM pog_check WHERE storenum = ". $row_dmstore['store'] ."";
$store = mysql_query($query_store, $conner) or die(mysql_error());

while($row_store = mysql_fetch_array($store)){
$projects_array[] = $row['pog_type'];
}
$string = explode(", ", $projects_array);

echo "Store#". $row_dmstore['store'] ." = ". $string ."<br />\n";
}
?>
[/code]
Link to comment
Share on other sites

Still coming up with the same thing my friend. Here is the page:

[url=http://www.connerandassociates.com/pog_check/man/test_print2.php]http://www.connerandassociates.com/pog_check/man/test_print2.php[/url]

Im a bit confused about this

[code]$string = explode(", ", $projects_array);[/code]

Wouldnt that turn it into an array?
Link to comment
Share on other sites

hmmm... well you're not getting an infinite loop of the same store, anymore which is good.

yes you are right... it should be implode, not explode. just replace this:[code]$string = explode(", ", $projects_array);[/code]

with this:[code]$string = implode(", ", $projects_array);[/code]

and see what happens.

Link to comment
Share on other sites

ok the store tables is something like this

dmrmst:

rm---------------dm-------------[color=red]store[/color]
name------------name-----------store#


2nd one is like this:

auditready,followup,poponorder,displayorder,other,explain,date,pogtype,[color=red]storenum[/color],num,dm
Link to comment
Share on other sites

what's the name of your second table? is it pog_check?


and don't forget that you need to reset OR unset $projects_array:
[code]
<?php
require_once('../../Connections/conner.php');

$dm = $_GET['dm'];
mysql_select_db($database_conner, $conner);

$query_dmstore = "SELECT * FROM dmrmst";
$dmstore = mysql_query($query_dmstore, $conner) or die(mysql_error());

$totalRows_dmstore = mysql_num_rows($dmstore);
        echo "Total number of stores: ". $totalRows_dmstore ."<br />\n";

while($row_dmstore = mysql_fetch_array($dmstore)){
$query_store = "SELECT * FROM pog_check WHERE storenum = ". $row_dmstore['store'] ."";
$store = mysql_query($query_store, $conner) or die(mysql_error());

                $projects_array = array(); //<--- you can reset it here
while($row_store = mysql_fetch_array($store)){
$projects_array[] = $row['pog_type'];
}
$string = explode(", ", $projects_array);

echo "Store#". $row_dmstore['store'] ." = ". $string ."<br />\n";

                unset($projects_array);  //<---- OR you can reset it here.
}
?>
[/code]
Link to comment
Share on other sites

well I tried something there,. but changed it back. Also it is now working but not display the "pog_types" just the commas.

EDIT

Yeah I just changed that but still the same

this is what I have:

[code]<?php
require_once('../../Connections/conner.php');

$dm = $_GET['dm'];
mysql_select_db($database_conner, $conner);

$query_dmstore = "SELECT * FROM dmrmst";
$dmstore = mysql_query($query_dmstore, $conner) or die(mysql_error());

$totalRows_dmstore = mysql_num_rows($dmstore);
        echo "Total number of stores: ". $totalRows_dmstore ."<br />\n";

while($row_dmstore = mysql_fetch_array($dmstore)){
$query_store = "SELECT * FROM pog_check WHERE storenum = ". $row_dmstore['store'] ."";
$store = mysql_query($query_store, $conner) or die(mysql_error());

                $projects_array = array(); //<--- you can reset it here
while($row_store = mysql_fetch_array($store)){
$projects_array[] = $row_store['pog_type'];
}
$string = implode(", ", $projects_array);

echo "Store#". $row_dmstore['store'] ." = ". $string ."<br />\n";

                unset($projects_array);  //<---- OR you can reset it here.
}
?>[/code]
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.