Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: Re: Tough question, To Bill and Nik pls your comentaries


Message #1 by "jorge" <jorge@d...> on Sun, 13 Oct 2002 08:18:35 -0500
Hi bill and Nik :)
this one doesnt have a nested query loop so this one should work faster Nik
your coments are necesary if you please so i keep learning thanks??
AS some one had mentioned before(Nik i believe), the easy solution is to add
a tablejust with the categories where category is unique this way the query
will be simple table one could be the unique category one, you can use as
many tables or variables as desired ........ thats why i wrote some dots.and
even count your categories the code is not complete since i dont know if
your want it, but the missing parts are very easy

like
$quer=mysql_query("SELECT t1.category,COUNT(category) AS my_counter,
t2.myvar_x,t3.myvar_y,....... FROM mycategoryuniquetable AS t1,mytable2 AS
t2, mytable3 AS t3,..... WHERE t1.mycategoryuniquetable = t2.myvar_x AND
t1.mycategoryuniquetable = t3.mycategoryuniquetable ORDER BY
t1.mycategoryuniquetable ASC");

//then make them arrays again
while($results=mysql_fetch_array($query))
    {
    $category[]=$results["category"];
    $myvar_x[]=...............
    $....
    }
//in this case we dont count the elementes of the array is useless
//this is to start the counter that we will use to loop through the array:
$i=0;
foreach ($category AS $key)
    {
        switch($key)
            {
            case "my_first_category":
            //this will print your categories line
              echo("<option>".$key."</option>");
                //this loops to the array of the other info
                while($key=="my_first_category")
                    {

echo("<option>&nbsp;&nbsp;&nbsp;".$myvar_x.[$i]"</option>");
                   $i++;
                    //this will get you the next element of the array that
is within the "my_first_category"
                    }//end while
    default:
       }//end of switch
    }//end for each

//ACTUALLY i will do this way the switch so i wouldnt have to write all the
cases of course may be the swithc is not necesary at all but this is just an
idea

 foreach ($category AS $key)
    {
        switch($key)
            {
            case "Jorge":
            break;
            default:
            //this will print your categories line
              echo("<option>".$key."</option>");
                //this loops to the array of the other info
                while($key=="my_first_category")
                    {

echo("<option>&nbsp;&nbsp;&nbsp;".$myvar_x.[$i]"</option>");
                   $i++;
                    //this will get you the next element of the array that
is within the "my_first_category"
                    }//end while
    default:
       }//end of switch
    }//end for each



        Saludos

Jorge


----- Original Message -----
From: "jorge" <jorge@d...>
To: "professional php" <pro_php@p...>
Sent: Friday, October 11, 2002 6:30 PM
Subject: [pro_php] Re: Tough question, involving arrays from MySQL


> hi Bill
> Just trying to help you
> You can do it with a two queries one nested inside the other, the first
> query will get you the categories an if category is not unique you can use
> DISTINCT.
> an use that category to loop to the subcategories even if they are on the
> same table.
> you can do it like this an excuse me if this is not orthodox
> (I have learn by myself)
> option 1
> $query=mysql_query("SELECT DISTINCT categories FROM mytable ORDER BY
ASC");
> while($results=mysql_fetch_array($query))
>     {
>     $category[]=$results["categories"];
>     }
> now you have an array with all of your categories order alphabetically.
> you can query the same database to obtain de sub categories
> <select name="my name">
> $howmany=count($category);
> for($i=0;$i<$howmany;$i++)
>     {
> //this line will print your categories
>  echo("<option value= \" category[$i] \ ">".$category[$i]."</option>)
> //withtin this actual category you get all subcategories
> $query=mysql_query("SELECT DISTINCT sub_categories FROM mytable WHERE
> catergories = '$category[$i]' ORDER BY ASC");
> while($results=mysql_fetch_array($query))
>     {
>     $categories=$results["categories"];
>     echo("<option value=\ "$category[$i].$categories
> \">".$categories"</option>)
>     }
> }// end for
> </select>.....
>
>
>
>
> ----- Original Message -----
> From: "Bill Bob" <bige88fan@c...>
> To: "professional php" <pro_php@p...>
> Sent: Friday, October 11, 2002 10:14 PM
> Subject: [pro_php] Tough question, involving arrays from MySQL
>
>
> > What I am trying to do is make a drop down box with categories and sub
> > categories. My main goal in my script is to display the Main Category,
> > with the sub-category in a logical order in the drop down box. Here is a
> > layout of what I mean:
> >
> > -> Main Category
> > .subcategory
> > .subcategory
> > .subcategory
> > etc
> > etc
> > etc
> > ->Another Main Category
> > .subcategory
> > .subcategory
> > .subcategory
> > etc
> > etc
> > etc
> >
> >
> > Know what I mean? I want it in tha logical order so that each sub
> > caetgory which belongs to the main category will be displayed directly
> > under the main category in the drop down box. Here is my code so far,
> > doesn't seem to work in the least bit:(------------------>
> >
> > //Start to get and echo out form categories.
> > $parents = mysql_query("SELECT title FROM cat_headings");
> > $rows = mysql_num_rows($parents);
> > if($rows==0){
> > die("Sorry, but we couldn't find any sub/categories!");
> > }
> > unset($rows);
> > while($parent = mysql_fetch_array($parents)){
> > $parent2["$parent[title]"] = $parent;
> > echo $parent2["$title"];
> > }
> > unset($parent);
> >
> > $subcats = mysql_query("SELECT
> > t1.headingid,t2.catname,t2.catprefix,t2.headingid FROM cat_headings AS
> > t1, categories AS t2 WHERE t1.headingid=t2.headingid");
> > $rows = mysql_num_rows($subcats);
> > if($rows==0){
> > die("Sorry, but we couldn't find any sub/categories!");
> > }
> > unset($rows);
> > while($subcat = mysql_fetch_array($subcats)){
> > echo $subcat[1][catname];
> > printr($subcat2, '$subcat2 is');
> > }
> > unset($subcat);
> >
> > echo "<select name=\"file[]\"><option selected value=\"\">[>Select File
> > Category<]</option>";
> >
> > while ( list($key,$parent)=each($parent2) ) {
> > echo "<option value=\"\">-->$parent</option>";
> >
> > while ( list($prefix,$name)= each($subcat2) ) {
> > echo "<option value=\"$prefix\">$name</option>";
> >
> > }
> > }
> >
> > echo "</select>";
> >
> >
> > It would be amazing as always if I can get this thing working. I am
sorry
> > to be a pest, but I wanna be a real pro someday.
> >
> > -Bill
> >
>
>
>



  Return to Index