/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Troubles with array output :/

Okay..

So I have this code..

[code=php]
$cPathArray = explode(“_”, $_GET[‘cPath’]);
$cPathArrayCount = count($cPathArray);

$row = 0;
for ($i=0;$i<$cPathArrayCount;$i++) {
$query = “SELECT categories_name, parent_id FROM categories_description
JOIN categories ON (categories_description.categories_id = categories.categories_id)
WHERE language_id = 1 AND categories_description.categories_id = “.$cPathArray[$i];
$category_query = mysql_query($query);
$category = mysql_fetch_assoc($category_query);

//$CategoryName.’_’.$row = $category[‘categories_name’];
$CategoryThingy = $CategoryThingy.’,’.$row.’_’.$category[‘categories_name’];
echo $CategoryThingy;

$row++;
}
[/code]

And it outputs this comma delimited list for example

[code],0_NCAA,0_NCAA,1_Apparel[/code]

How come it does the first one twice? ? .. ie: 0_NCAA, 0_NCAA when if I just echo the $category[‘categories_name’] it doesn’t duplicate them..

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@cinematic_jesiauthorDec 16.2009 — nevermind, I fixed it.
Copy linkTweet thisAlerts:
@criterion9Dec 16.2009 — I think there is a much easier way to do this:

$cPathArray = explode("_", $_GET['cPath']);

$cPathArrayCount = count($cPathArray);

$row = 0;

for ($i=0;$i<$cPathArrayCount;$i++) {

$query = "SELECT categories_name, parent_id FROM categories_description

JOIN categories ON (categories_description.categories_id = categories.categories_id)

WHERE language_id = 1 AND categories_description.categories_id = ".$cPathArray[$i];

$category_query = mysql_query($query);

$category = mysql_fetch_assoc($category_query);

//$CategoryName.'_'.$row = $category['categories_name'];

$CategoryThingy = $CategoryThingy.','.$row.'_
'.$category['categories_name'];

echo $CategoryThingy;

$row++;

}
[/quote]

Something like this would be much more efficient (untested):
[code=php]
$cPathArray = str_replace("_",",",$_GET['cPath']);

$query = "SELECT categories_name, parent_id FROM categories_description
JOIN categories ON (categories_description.categories_id = categories.categories_id)
WHERE language_id = 1 AND categories_description.categories_id IN (".$cPathArray.")";
$category_query = mysql_query($query);

$row=0;
$CategoryThingy = '';
while($category = mysql_fetch_assoc($category_query){
$CategoryThingy .= ','.$row.'_'.$category['categories_name'];
$row++;
}
echo $CategoryThingy;
[/code]

Then to fix your duplicates problem you can add a GROUP BY clause to your query.
Copy linkTweet thisAlerts:
@cinematic_jesiauthorDec 16.2009 — Yeah, I fixed it with this:

[code=php]if ($_GET['cPath'] > 0) {

$cPathArray = explode("_", $_GET['cPath']);
$cPathArrayCount = count($cPathArray);

// now we need to do a loop to get all the data :)
for ($i=0;$i<$cPathArrayCount;$i++) {
$query = "SELECT categories_name, parent_id FROM categories_description
JOIN categories ON (categories_description.categories_id = categories.categories_id)
WHERE language_id = 1 AND categories_description.categories_id = ".$cPathArray[$i];
$category_query = mysql_query($query);
$category = mysql_fetch_assoc($category_query);

$CategorySubTitle .= $category['categories_name'].' &raquo; ';
}[/code]


Just having a dumby moment haha thanks!
Copy linkTweet thisAlerts:
@criterion9Dec 16.2009 — Your code still creates a potentially unlimited number of calls to the DB where mine makes a single query. This probably wouldn't be an issue for a small site but that many queries will potentially create problems if the site starts to grow.
×

Success!

Help @cinematic_jesi 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.21,
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,
)...