SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    either my mind is not working or what I'm not sure, but I want to do this...

    I have a table (example only)

    Code:
    ID | Weight | Desc
    --------------------------
    1  | 25     | Boat
    --------------------------
    2  | 134    | Dog Kennel
    --------------------------
    3  | 25     | Flour
    --------------------------
    4  | 25     | Dog Food
    --------------------------
    5  | 134    | Raft
    --------------------------
    6  | 269    | Step Ladder
    ---------------------------
    And I would want to display a page, where it displayed hyperlinks, and each hyperlink would come from a MySQL query... I want to display something like this...

    Code:
    Select Weight Of Object
    
    25 (3)
    134(2)
    269(1)
    All I have been able to come up with is something that displays this

    Code:
    Select Weight Of Object
    
    25
    25
    25
    134
    269
    After I get it the way I want, I could then use a variable to pass along the weight of the object, and display with a WHILE statement all of the objects with the weight of 25, 134, 269, etc....

    Please advise..

    God Bless, and when I finish this project I'll help out all I can, untill then, please have grace upon me!

    Thanx again to FreddydoesPHP and TWTCommish, and to everyone else, you've been a tremendous help!
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this SQL,

    SELECT Weight, COUNT(*) AS num
    FROM <yourTableName>
    GROUP BY Weight

  3. #3
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm sorry... could you please throw that into a basic WHILE for me... so I can see what you're talking about...

    thanx for help, in the meantime I'll attemt to tackle it..
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    exbabylon: Could you explain exactly what it is you need? I'm a little confused even after reading this thread twice. I semi-understand, but if you can give me an example of what you would like, I'd be glad to help.

    I'll do my best to make it highly personalized - if you can throw in the table and field names you want, specifically, I might be able to help.

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The sql when used in a mysql_query() will return the result set you were after.

    25 (3)
    134(2)
    269(1)


    Code:
    $sql = "SELECT Weight, COUNT(*) AS num 
            FROM <yourTableName> 
            GROUP BY Weight";
    
    $result = mysql_query($sql);
    
    while($row = mysql_fetch_object($result) {
       echo("<a href='$PHP_SELF?weight=$row->Weight'".
            $row->Weight."(".$row->num.")</a><br>");
    }
    This will produce the list of hyperlinks you require with each link passing the appropriate weight value in the variable $weight.

    So when the page is called you will need something like:

    Code:
    if ($weight) {
       $sql = "SELECT ...
               FROM <yourTableName>
               WHERE Weight = '$wieght'";
    
       $result = mysql_query($sql);
    
       //loop through the result set to create the hyperlinks for each item.
    }

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice post, freaky.

    Small comment, though: since exbabylon seems to be in the process of familiarizing himself with the basics of PHP/MySQL, would it necessarily be a good idea to introduce him to objects right now? I'd personally recommend mysql_fetch_array in his case for now.

    After all, the mighty Kevin Yank himself used it in his massive tutorial.

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    np - its a matter of personal preference. I find that $row->Weight is easier to user because you don't end up with quotation mark confusion in your echo string.

    ok so substitute "mysql_fetch_object" with "mysql_fetch_array"

    and substitute $row->Weight with $row["Weight"].

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a good point I usually declare variables to help this (although I'm sure it slows things down at least a tiny bit):

    $var = $row["var"];

    Your way is better, IMO, just dunno if he'll be as overwhelmed with OOP as I've been. Better overall though, I'll have to remember it.

  9. #9
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My personal preference is using mysql_fetch_array along with printf() if variables need to be outputed. It seems inefficient to create a new variable ($foo = $bar['ID']) when there is already another variable that contains the same contents.

  10. #10
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have a background in c. So oneday, I'll get around to reading up about printf in the manual In the mean time, could you post the code you would need to substitute your printf method in the code I gave above?

  11. #11
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    printf is pretty simple - you can use single quotes to define your string of text/HTML/code/etc so that double quotes do not need to be escaped.

    Example:

    Code:
    printf('Hi there! <a href="page.html">This is a link</a>');
    I believe you display variables in a different way, though:

    Code:
    printf('Hi there, %s! <a href="page.html">This is a link</a>', $name);
    As you can see, the variables are declared after you specify the string of text - in this case, the only variable at the end, $name, will be insert in place of "%s" - the "s" signifies a string. %d would be used for a double/floating point integer, I think.

  12. #12
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you all... you've been a tremendous help....

    And thank you freakysid, I have been wondering how objects work... now I got it... very thankful.

    I need to learn to step out of the box more I think, I'm still stuck in it... having a hard time, so thanx for the patience.

    I have been continuing to work on my project, and have run into yet another problem...

    I am *hoping* to get around 500 entries in this database in the near future... so I have run into a problem...

    What I am working on is a Load Data Database, so far I've got username, password, profile, add a load, add a cartridge, etc. My problem now is how to display the cartridges. If I have several hundered, it would get extreemly anoyingly long... so, I've decided to break them up into several pages... great idea right? Well, I'm still in my box, and mabey someone would help me out...

    I would like to get a three colum thing going...

    Code:
    22 Caliber | 30 Caliber    | 35 Caliber 
    25 Caliber | 30-06 Caliber | 416 Caliber
    obviously sorted... but I have not figured out a way yet to accomplish this. What would be ideal is for it to only display one colum until it gets the the set length, then go on... this could be done with several tables, but I'm just not sure.

    So if there were only two..

    Code:
    22 Caliber
    25 Caliber
    if three

    Code:
    22 Caliber |30 Caliber
    25 Caliber |
    if five

    Code:
    22 Caliber | 30 Caliber    | 35 Caliber 
    25 Caliber | 30-06 Caliber |
    Id this a possiblity?

    Also, lets say I set it to run for twenty rows, and twenty colums, and it ran out of room... how about a next page?

    Thank you for your time, and if this is unfeesable, let me know.. thank you again for your help, sitepoint (and webmasterbase) have helped me sooo much. I just started three weeks ago with Kevin's Tutorial, and the learning curve has been vertical!

    God Bless you all,

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  13. #13
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, no one has taken this on. It will require a bit of code. So, to get the ball rolling I'll give you a bit of pseudo-mumbo-jumbo to flesh out the logic of how I would do this off the top of my head.

    1. Query the database to count how many items you got all up. Save it in $totalItems.

    2. Query the database to return the item names, ordered by name, but limit the result set to say $X items.

    The SQL is something like...

    SELECT name
    FROM Items
    ORDER BY name
    LIMIT $offset, $X

    3. What is $offset? Well, the first time you use this sql $offset should have a value of 0 (zero) because you want the result set to start at the first record. But I'll come back to this at the end.

    4. Now you need to work out how many items your result returned. something like...

    $numItems = mysql_num_rows($result);

    5. Work out how many columns you need. Now logically, if you are going to have a max of three columns in your table then $X must be divisable by 3 (say 15 or 27 or 36 etc) Lets say $X is 15, therefore $maxPerCol = 15/3 = 5.

    To work out how many columns...
    Code:
    if ( $numItems % $maxPerCol == 0) {
       $neededCols = $X / $maxPerCol;
    } else {
       $neededCols = (int)($numItems / $maxPerCol) + 1;
    }
    Ok. Now you know how many columns you need.

    Eg, $numItems = 12

    12 % 5 = 3
    therefor use

    $neededCols = (int)(13/5) + 1 = 3

    6. Next, get the data into a seperate array for each column.
    Code:
    $i = 0;
    $j = 0;
    $k = 0;
    $x = 0;
    
    while($anItem = mysql_fetch_array($result) {
       if ( $x < $maxPerCol ) {
          $column1[$i++] = $anItem["name"];
       } else {
          if( $x < 2*($maxPerCol) ) {
             $column2[$j++] = $anItem["name"];
          } else {
             $column3[$k++] = $anItem["name"];
          }
       }
       $x++;
    }
    7. Now you can draw your table
    Code:
    echo "<table>";
    
    $x = 0;
    
    for each($column1) {
       echo "<tr><td>$column1[$x]</td>";
       if ( count($column2) >= $x ) {
           echo "<td>$column2[$x]</td>";
       }
       if ( count($column3) >= $x ) {
           echo "<td>$column3[$x]</td>";
       }
       echo "</tr>";
       $x++;
    }
    echo "</table>";
    Because this is off the top of my head that code may not work - there might be other issues to do with table formating. But you get the jist of my [il]logic.

    8. But wait there's more! Step 1 of this algorithm was to work out how many items there are in total. So you should be able to work out how many pages you will need in total to display all items.
    Code:
    if ( $totalItems % $X == 0) {
       $neededPages = $totalItems / $X;
    } else {
       $neededPages = (int)($totalItems / $X) + 1;
    }
    So now you need to add code to generate a link to the next page (if there is one; ie,
    Code:
    if ($neededPages > $thisPage) {
       // you need more pages
       $nextPage = $thisPage + 1;
       echo "<a href='$PHP_SELF?page=$nextPage'>next page</a>";
    }
    Now when this page is called again it receives the value of $page - ie the page to display.

    So you need to do the following between step 1 & 2 above
    Code:
    if( $page ) {
       $thisPage = $page;
    } else {
       $thisPage = 0;
    }
    
    $offset = $X * $thisPage;
    Hope this gives you some ideas. I don't say its correct or the best way - just my thoughts that might help you think through the logic of what to do.
    Last edited by freakysid; Feb 12, 2001 at 09:35.

  14. #14
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow...

    I sure did not expect such a major and in-depth response.

    I am still new at this... as you have seen, but I think I can manipulate what you gave me to work...

    You got me out on the right track...

    God Bless you,

    alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services


Bookmarks

Posting Permissions

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