Thread: Table Problem

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0

    Table Problem


    I have a start to this but I seem to be tripping myself up. These were the instructions for this script:
    Create an input form, html, which will ask for two numbers from the visitor, the start of the inventory ID range and the end. Within the script, define two constants - one to hold the lowest number assigned to the ID range by your organization, the other to hold the highest. NOTE: The idea here is that the organization may not wish to start number at 1. Read the numbers entered by the visitor and verify that they are sensible - the end of the range must be larger than the start, the starting number must be larger (or equal to) the lowest number in the ID range, and the ending number must be smaller than the highest ID assigned. Establish a while loop which will count from the start of the requested range to the end. Within the body of that loop, build a text string which will present the ID number and the inventory number, using a separate line (you can do this with a br tag) for each item. For now, you may assign a fixed value to the inventory number for each item - 8 items. Use a variable to hold that value, because in reality the value should be read from the database. In the HTML portion of the script file, display the result string.

    PHP Code:
    <?php
        define
    ("LO_BOUND"1);
        
    define("HI_BOUND"8);
        
    srand(1);
        
    $start $_GET["start_search"];
        
    $end $_GET["end_search"];
        if ((
    $start LO_BOUND) || ($start HI_BOUND))
        {
            
    header("Location: solution05.html");
        }
        if ((
    $end LO_BOUND) || ($end HI_BOUND))
        {
            
    header("Location: solution05.html");
        }
        
    $table "<table border=\"2\">";
        
    $currentRow 1;
        while (
    $currentRow <= $start)
        {
            
    $table .= "<tr>";
                
    $currentColumn 1;
                while (
    $currentColumn <= $end)
                {
                    
    $table .= "<td>";
                    
    $data rand(LO_BOUNDHI_BOUND);
                    
    $table .= "Cell Data: " $data;
                    
    $table .= "</td>";
                    
    $currentColumn $currentColumn 1;
                }
            
    $table .= "</tr>";
            
    $currentRow $currentRow 1;
        }
        
    $table .= "</table>";
    ?>
    <html>
    <head>
    <title>Batty League Flea Market</title>
    </head>

    <body>
    <h1>Here's what's in stock!</h1>
    <?php echo $table?>
    </body>
    </html>
    start_search and end_search are the two inputs from the HTML page, and the IDs start at 1 and go to 8. I don't see any syntax errors, but the first issue is that a chunk of code is being displayed above "Here's what's in stock." I'm searching in my book but this wasn't from any specific chapter so I'm trying to make sense of it again.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,540
    Rep Power
    2337
    a chunk of code is being displayed above "Here's what's in stock."
    What's the "chunk of code"?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Originally Posted by ptr2void
    What's the "chunk of code"?
    HI_BOUND)) { header("Location: solution05.html"); } if (($end < LO_BOUND) || ($end > HI_BOUND)) { header("Location: solution05.html"); } $table = ""; $currentRow = 1; while ($currentRow <= $start) { $table .= ""; $currentColumn = 1; while ($currentColumn <= $end) { $table .= ""; $currentColumn = $currentColumn + 1; } $table .= ""; $currentRow = $currentRow + 1; } $table .= "
    "; $data = rand(LO_BOUND, HI_BOUND); $table .= "Cell Data: " . $data; $table .= "
    "; ?>
    Here's what's in stock!


    so it looks like it starts on line 7.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Since we just did databases, and can pull information from a database and enter new information into it, I'm really struggling. I can't get past what is causing the display error. If it would display right, I could go from there trying to figure out the additions i still need to make for the project.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    View the source of this page, I bet your whole PHP source is being shown as the page source.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    I'm not certain I understand.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    View this page via a web browser. Right-click within the page and select 'View Source'. Does this code appear within the presented code? If yes, your greater-than sign, shown below, is being processed as the closing tag for your PHP, which should not be.
    PHP Code:
    if (($start LO_BOUND) || ($start HI_BOUND)) 
    Is this a server of your own, or a host? School/Web Host/other... This is VERY wierd, since it shows that, stops showing PHP code for a short, then steps back in to show the closing of the PHP code.

    Can you perhaps post us what you receive from the View Source?
    Last edited by Triple_Nothing; March 2nd, 2013 at 08:54 AM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    And, as a side note, I would suggest not using rand() in your
    $data = rand(LO_BOUND, HI_BOUND);
    line. Since, in the end, this scripts goal is to reference items from the database. With your current setup, you may present duplicates, as well as have missing items, since rand() will just hand you any random number betwen the start/end you provide it. As far as your project instructions, it sounds they want you to have your script list the items from beginning to end. The while loop pretty much just needs to create visual design, and will enter the values of the next idexed item from the given array through each iteration.

    Comments on this post

    • mudpuppet agrees
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Thanks for your reply. I got on and tried to open the file but it refused to open. I put WAMPserver 2.2 offline, and then back online, and not only did it load, but the chunk of code is no longer appearing and the table is displaying at the bottom. The code is from a few weeks back and I've had the server up and down since then so I'm at a loss as to what happened. Also, yes it's for school and it's here at my home computer.

    So now that the display is working, the next thing to tackle is the rand(), and adding the stock amount. I added $inventory = "8"; to use and it is displaying in the cell so that is working now. What would you suggest I do, removing the rand()?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Location
    Manchester
    Posts
    10
    Rep Power
    0
    Just a couple of things since I'm guessing you're learning this at school...

    A couple of good practices...

    PHP Code:
    $currentColumn 1
    Since you're looping through the columns from 1, you should declare it as an integer as 1 also represents true.

    PHP Code:
    $currentColumn = (int) 1
    Also do this for 0 (zero) as zero also means false. No need for any other number.

    PHP Code:
    $currentColumn $currentColumn 1
    In a loop, when incrementing the value of something by 1, you can simply do this...

    PHP Code:
    $currentColumn++; 

    Comments on this post

    • Jacques1 disagrees : Sorry, but this is complete nonsense.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    I am and thank you for the tips. Do you have any advice on how to or where to look to remove the rand() from the script? If possible im sure it would be best to list the low to high, that is 2, 3, 4, 5, etc.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Ok, it displays properly now, however, I would like to have it list each ID only once. I understand that I need to remove the rand() however I'm lost as to what to put in its place.

    Here is the code now:
    PHP Code:
    <?php
        $start 
    $_GET["start_search"];
        
    $end $_GET["end_search"];
        
    $low "1";
        
    $high "8";
        
    $step "1";
        
    $inventory "8";
        
    srand(1);
        if ((
    $start $low) ||
            (
    $start $high)) {
            
    header("Location: solution.html");
        }
        if ((
    $end $low) ||
            (
    $end $high)) {
            
    header("Location: solution.html");
        }
            
    $table "<table border=\"2\">";
        
    $currentRow 1;
        while (
    $currentRow <= $start) {
            
    $table .= "<tr>";
                
    $currentColumn = (int) 1;
                while (
    $currentColumn <= $end) {
                    
    $table .= "<td>";
                    
    $data rand($start$end);
                    
    $table .= "Item ID: " $data;
                    
    $table .= "<br />";
                    
    $table .= "#In Stock: " $inventory;
                    
    $table .= "</td>";
                    
    $currentColumn++;
                }
            
    $table .= "</tr>";
            
    $currentRow $currentRow 1;
        }
        
    $table .= "</table>";
    ?>
    <html>
    <head>
    <title>Batty League Flea Market</title>
    </head>

    <body>
    <h1>Here's what's in stock!</h1>
    <?php echo "$table"?>
    </body>
    </html>
  24. #13
  25. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,924
    Rep Power
    1045
    What the hell are you talking about, SWH Admin? 0 and 1 are integers, not booleans. Neither are they identical to nor do they "represent" false and true. Why would you even say something like that?

    PHP does have weak typing and will automatically convert numbers into booleans if needed. But that doesn't change the fact that they're two completely different data types. And the type conversions apply to any value, not just 0 and 1. 123, 42.5, "Justin Bieber", array(-1) are all converted to true, while 0, 0.0, "" and array() are converted to false.

    Casting the integer 1 into an integer has absolutely no effect at all and shows a deep misunderstanding of data types in PHP.

    If you're a new to PHP and don't have a firm understanding of it yet, it would be great if you'd not tell people about "good practices".
    Last edited by Jacques1; March 2nd, 2013 at 03:13 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Location
    Manchester
    Posts
    10
    Rep Power
    0
    Ok, here's how I understand the question set up above...

    1) Define the start and end range values of the ids
    Kept them as "defined" constants since the question appears to ask for them to be defined this way.

    2) the end of the range must be larger than the start
    I added in to redirect to solution.html if the start is higher than the end as before the script didn't take into account if start was higher than end.

    3) Establish a while loop which will count from the start of the requested range to the end
    I rewrote your loop to count from start to end.

    4) Within the body of that loop, build a text string which will present the ID number and the inventory number
    Now the script just prints out the id value ($start either as provided by user or after it's been incremented) and the number of items (set to 8 as declared by $inventory).

    And here's the script without pulling anything random...

    PHP Code:
    <?php
        $start 
    $_GET["start_search"];
        
    $end $_GET["end_search"];
        
    define('LOW'1);
        
    define('HIGH'8);
        
    $inventory "8";
        if (
    $start $end) {
            
    header("Location: solution.html");
        }
        if ((
    $start LOW) ||
            (
    $start HIGH)) {
            
    header("Location: solution.html");
        }
        if ((
    $end LOW) ||
            (
    $end HIGH)) {
            
    header("Location: solution.html");
        }
            
    $table "<table border=\"2\">";
        while (
    $start <= $end) {
            
    $table .= "<tr>";
                    
    $table .= "<td>";
                    
    $table .= "Item ID: " $start;
                    
    $table .= "<br />";
                    
    $table .= "#In Stock: " $inventory;
                    
    $table .= "</td>";
            
    $table .= "</tr>";
            
    $start++;
        }
        
    $table .= "</table>";
    ?>
    <html>
    <head>
    <title>Batty League Flea Market</title>
    </head>

    <body>
    <h1>Here's what's in stock!</h1>
    <?php echo "$table"?>
    </body>
    </html>
    If I've completely got it wrong I apologize...

    @Jacques

    I've been studying PHP for a good few years. Sadly I wasn't given the opportunity to study it at school so I'm self taught. I don't understand the technicalities and assume (perhaps wrongly) that anything someone else has told me (or perhaps something I've read) to be correct. In this case, I remember seeing somewhere that (int) should go before 1 if it's to be used solely as an integer otherwise it could be treated as true which apparently could mean something else as far as the script is concerned. I'm open minded though so I won't argue your case. I'm right about the increment part though
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Thank you so much for you help and explanation.

    So
    PHP Code:
    define('LOW'1); 
    defines LOW as a constant, where as I had $low as a variable. I get that part, and it follows the directions though I'm a bit unsure why one is better than the other is the variable is not being changed. Or is it just that, because it isn't meant to change it "should" be written as a constant.

    The redirect I hadn't even thought of though it makes sense. I suppose if I were doing more work to this I could have it redirect back and display an error message.

    The while loop makes sense and it adds +1 to the start number each time, to keep it going. After start is no longer <= end, the loop stops.

    Thank you so much for your help. This was something that had been drawn up weeks ago and was stumped when things weren't displaying. I had this code and wanted to work on it again, but couldn't figure out how I had even gotten to where I was as there was no chapter that went right along with this and the posted examples refused to download to look at. I really do appreciate all the help everyone has given and I can look at this code and understand what it does. The hardest part is remembering it all for the next time and storing each part in my head so that I won't have this hard of a time next time trying to make sense of what's there and what needs to be.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo