/    Sign up×
Community /Pin to ProfileBookmark

logic help with while/if loops

I am trying to list products under headings, but when I run this, I get the right headings, but the sub categories under the headings just duplicate. How can I fix this?

[code=php]<?
$query = mysql_query(“SELECT *
FROM product_list_main”);
if (mysql_affected_rows() > 0) {
while($row = mysql_fetch_array($query)){
$cat = $row[‘main_category’];
// Heading for the section
echo ‘<h2>’.strtoupper ($cat).'</h2>’;
$query2 = mysql_query(“SELECT *
FROM product_list_sub
WHERE mainProdID = ‘$cat'”);
if (mysql_affected_rows() > 0) {
while($row2 = mysql_fetch_array($query2)){
$description = $row2[‘description’];
$name = $row2[‘name’];
$prodID = $row2[‘id’];
# All of the sub-items listed here
?>
<? echo $name; ?> [<a href=”#” rel=”modal-profile” id=”<? echo $prodID; ?>” class=”product_quickview”>QUICKVIEW</a>]<br />
<?
}
}
}
}
?>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowOct 29.2014 — 1) might help if your indentation scheme made the least bit of sense.

2) This is 2014 not 2006, why are you using the deprecated and soon to be not supported mysql_ functions? [i]See the [url=http://php.net/manual/en/function.mysql-connect.php]GIANT RED WARNING BOXES IN THE MANUAL[/url] to that end.[/i]

3) you aren't doing anything that would need to trap the affected_rows, in fact that shouldn't even be set with a SELECT function.

4) avoid slow double quote strings where possible.

5) don't make variables for nothing.

6) fantasy-land rel attribute probably means there's deeper rooted issues in how this works.

7) a single div around the anchors with a class or ID would probably be better and less code in the output than slapping a class on every single matching anchor.

IF one were to keep the outdated mysql functions...

&lt;?

$query = mysql_query('
SELECT *
FROM product_list_main
');

while ($row = mysql_fetch_array($query)) {

<i> </i>echo '

<i> </i> &lt;h2&gt;', strtoupper($row['main_category']), '&lt;/h2&gt;
<i> </i> &lt;div class="product_quickviews"&gt;';

<i> </i>$query2 = mysql_query('
<i> </i> SELECT *
<i> </i> FROM product_list_sub
<i> </i> WHERE mainProdID = <span><code>' . $row['main_category'] . '</code></span>
<i> </i>');

<i> </i>while ($row2 = mysql_fetch_array($query2)) echo '
<i> </i> ', $row2['name'], '[&lt;a href="#" id="', $row2['id'], '"&gt;QUICKVIEW&lt;/a&gt;]&lt;br /&gt;';

<i> </i>echo '
<i> </i> &lt;!-- .product_quickviews --&gt;&lt;/div&gt;';

}

?&gt;


Is probably what you are trying to do... not that those pointless hash's in the href are all that useful, or is that abuse of the rel attribute some sort of inaccessible scripttardery? Remember, if you can't make it work without scripting FIRST, you likely have no business adding scripting to it.

This is an excellent case for when PDO is 'better' at doing things:

&lt;?

/* assumes $db is a connected PDO object */

if ($stmt = $db-&gt;query('
SELECT main_category
FROM product_list_main
')) {

<i> </i>$subStmt = $db-&gt;prepare('
<i> </i> SELECT id, name
<i> </i> FROM product_list_sub
<i> </i> WHERE mainProdID = ?
<i> </i>');

<i> </i>while ($cat = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {

<i> </i> echo '

<i> </i> &lt;h2&gt;', strtoupper($cat['main_category']), '&lt;/h2&gt;
<i> </i> &lt;div class="product_quickviews"&gt;';

<i> </i> $subStmt-&gt;execute([$cat['main_category']]);

<i> </i> while ($sub = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) echo '
<i> </i> ', $sub['name'], '[&lt;a href="#" id="', $sub['id'], '"&gt;QUICKVIEW&lt;/a&gt;]&lt;br /&gt;';

<i> </i> echo '
<i> </i> &lt;!-- .product_quickviews --&gt;&lt;/div&gt;';

<i> </i>}

}

?&gt;


In addition to providing auto-sanitation, prepared queries also let you prepare once and then plug in the values on execute multiple times.

SOME people would tell you to use a 'join' so there's only one query and table result; that's fine if you've got memory to burn, most people don't which is why I prefer the approach you used of one query followed by many.
×

Success!

Help @droidus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...