www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Troubles with array output :/

  1. #1
    Join Date
    Feb 2008
    Location
    Boondocks, Ohio
    Posts
    127

    resolved [RESOLVED] Troubles with array output :/

    Okay..

    So I have this code..

    PHP Code:
    $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++;

    And it outputs this comma delimited list for example
    Code:
    ,0_NCAA,0_NCAA,1_Apparel
    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..
    Last edited by cinematic_jesi; 12-16-2009 at 09:24 AM.

  2. #2
    Join Date
    Feb 2008
    Location
    Boondocks, Ohio
    Posts
    127
    nevermind, I fixed it.

  3. #3
    Join Date
    Jan 2009
    Posts
    3,346
    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++;
    }
    Something like this would be much more efficient (untested):
    PHP Code:
    $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
    Then to fix your duplicates problem you can add a GROUP BY clause to your query.

  4. #4
    Join Date
    Feb 2008
    Location
    Boondocks, Ohio
    Posts
    127
    Yeah, I fixed it with this:

    PHP Code:
    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; ';

    Just having a dumby moment haha thanks!

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles