Jump to content

Retrieve Highest Value


lil_bugga

Recommended Posts

Hi guys, I posted a topic a few days ago having problems with my navigation system, I've still had no replies on that to help so I'm trying a new way. I'm thinking of:

 

[*]1st creating $num to store highest id no. from store_category

[*]then creating $i = 0

[*]then starting loop whilst $i <= $num, start building display block[]

[*]then make a query to get store_category title that is the equivilant of $i

[*]then make a query to get any sub links

[*]incriment $I =$i +1

[*]end loop

 

each new iteration should now get the main link and any 2nd level links

then i should be able to display the blocks

 

The problem i'm having at the moment is retrieving the highest value, this is what I have so far

 

<?php
//connect to server and select database; you may need it
$mysqli = mysqli_connect("localhost", "xxx", "xxxxx", "test");

//show categories first

echo "php start<br /><br />";

$num = "SELECT MAX(id) FROM store_categories";
$res = mysqli_query($mysqli, $num);

if (res) {
echo $res;
}

echo "<br /><br />php end";

//close connection to MySQL
mysqli_close($mysqli);
?>

 

What i'm trying to achieve overall is a block like this, then where i have the options of beginners, kids and adults I could have many more options all taken from my database

 

                <a href="javascript:display('$id')" class="Group">~" $cat_title "~</a>

                <div class="hide" id="$id">

                    <a href="#" class="Option">Beginners</a>

                    <a href="#" class="Option">Kids</a>

                    <a href="#" class="Option">Adult</a>

                </div>

 

Thanks in advanced for any help

Link to comment
Share on other sites

Basically I have two tables for items in a mock shop

 

store_categories

id    cat_title    cat_desc 

1    Hats          Funky hats in all shapes and sizes!

2    Shirts        From t-shirts to sweatshirts to polo shirts and be...

3    Books        Paperback, hardback, books for school or play

 

and store_subcategories

subcat_id    cat_id    subcat_title 

1                3            Adult Books

2                3            Childrens Books

3                3            Educational Books

4                3            Reference Books

5                2            Mens Tops

6                2            Boys T-ops

7                2            Ladies Tops

8                2            Grils Tops

9                1            Causal Hats

10              1            Party Hats

11              1            Fishing Hats

12              1            Sports Hats

 

I'm attempting to load these so that I can have my navigation update just from the database so I can add and remove lines without all the effort.

 

When I was mocking up my site I was using javascript to manage my navigation and its different levels. This is the javascript navigation I had in place before hand, basically when i clicked the main link the div changes from hidden to visible.

 

<a href="javascript:display('Books')" class="Group">~ Books ~</a>

<div class="hide" id="Books">

    <a href="#" class="Option">Beginners</a>

    <a href="#" class="Option">Kids</a>

    <a href="#" class="Option">Adult</a>

</div>

 

I'm trying to get my SQL code to produce this block of code from me substituting the hard coded values like books from the example above with the categories from store categories table.

 

hope this helps make it clearer

Link to comment
Share on other sites

wait wait, didn't read your next post. Okay what you want to actually do is a join.

 

SELECT store_categories.cat_id, store_categories.cat_title, store_subcategories.subcat_title FROM store_categories, store_subcategories, WHERE store_categories.cat_id = store_subcategories.cat_id ORDER BY store_categories.cat_id DESC

 

and then you should get returned

 

cat_id  cat_title  subcat_title

3          Books    Adult Books

3          Books    Childrens Books

3          Books    Educational Books

3          Books    Reference Books

2          Shirts    Mens Tops

2          Shirts    Boys T-ops

2          Shirts    Ladies Tops

2          Shirts    Grils Tops

1          Hats      Causal Hats

1          Hats      Party Hats

1          Hats      Fishing Hats

1          Hats      Sports Hats

 

 

 

Link to comment
Share on other sites

...and what is wrong with that result?  a loop, couple of conditions to display certain things at certain times, and you've got exactly what you're asking for.

 

Please see my new topic, i think that demonstrates and explains what I want a lil more clearly, it just be me getting it wrong as im new to php.

 

http://www.phpfreaks.com/forums/index.php/topic,252495.msg1185812.html#msg1185812

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.