SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Newbie question - Code review request

    Hello,

    I am a web site programmer with many HTML sites under my belt. I am great with computers, the internet, and handling things such as CGI. However, this is my first time actually writing my own PHP site. I purchased Kevin Yank's book, and am working on my first PHP site.

    Essentially, what I am doing is making our portfolio database driven. So there will be a MySQL Database with fields such as name, description, category, thumbnail, etc. There will be the portfolio php file that displays categories and single entries.

    So far I have one question and one problem.

    Question:

    Passing variables from one page to another. So the first page in the portfolio will just do some blah blah about our work and quality of work, and on the left will be a menu with different choices for categories. When clicked, they will pass the value clicked to the same page. Right now, I am doing this by using the ?var= in the url. I read (and understand why) that this isnt the best way to do it, and using the post method is better. My question is, is there a way to use the POST method without a form, just on a link?

    Problem.

    So I have my portfolio.php. I want it, so that when no category is set, to display an intro splash. When a category is picked, display the name of the category (and Ill make it more detailed later. one step at at a time.)


    PHP Code:
    <?php
    if (isset($cat)):
    echo 
    "You have selected the following category: $cat. The portfolio is currently under construction";
    ?>
    <?php 
    else: 
    print <<< 
    END 
    <-- whole bunch of HTML code for a table and some niceformatted text for our intro text -->
    END

    endif;
    ?>
    For some reason, it dies on the line where the HTML code is:
    Parse error : parse error in /u/web/abacu9/portfolio2/portfolio.php on line 147
    I have tried both printing and echoing, with and without the here document. I have tried dumping the HTML code to a variable and then echoing. It doesnt want to work! Any ideas?

    I have tried searching the site/forums. I am sorry if there is an answer to these elsewhere.

    BTW, thank you for the awesome book and incredible site!

  2. #2
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this

    PHP Code:
    <?php
    if (isset($cat))(
    echo 
    "You have selected the following category: $cat. The portfolio is currently under construction";
    } else { 
    print 
    "PUT YOUR HTML CODE HERE";
    // END <-- whole bunch of HTML code for a table and some nice, formatted text for our intro text
    }
    ?>

  3. #3
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My first suggestion would be to use the standard forms for control structures, I.e
    PHP Code:
    if (condition) {
       
    //statement
    } else {
       
    //statement

    instead of what you're using as it lends to better readability.

    For your specific problem, I would break out of PHP for the HTML
    PHP Code:
    <?php
    if (isset($cat)) {
    echo 
    "You have selected the following category: $cat. The portfolio is currently under construction";
    } else { 
    ?>
    <!-- whole bunch of HTML code for a table and some nice, formatted text for our intro text -->
    <?php
    }
    ?>

  4. #4
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by anode
    For your specific problem, I would break out of PHP for the HTML
    PHP Code:
    <?php
    if (isset($cat)) {
    echo 
    "You have selected the following category: $cat. The portfolio is currently under construction";
    } else { 
    ?>
    <!-- whole bunch of HTML code for a table and some nice, formatted text for our intro text -->
    <?php
    }
    ?>
    First off, thank you for the quick and helpful replies.

    I had thought to break out of PHP for the HTML, but silly me I had been trying to put an end if in the closing section of the php, rather then leaving the else command open for the duration of the HTML. Thank you for pointing out the correct way.

    Now I can get back to building the app itself! Again, thank you for your help.

  5. #5
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you know, I'm back! I knew I would be

    Anyway, first I'll describe what I am trying to do, and then show the obviously wrong way I am going about it.

    OK, so I am throwing a search into MySQL that I know will have no more then 6 matches, and when it does, I'll limit it. Each search will have 9 rows that match. What I want to be able to do is have each of these in variables, so that I can play with the HTML formating to my hearts content, and have some in layers that I can toy with using javascript and what not.

    So if my rows were (and are):

    entryid
    client
    project
    industry
    media
    solution
    results

    Then in the end, I would get X amount of variables per row, where X is the number of resulting matches:

    entryid1, entryid2, entryid3, and so on.

    Am I making sense?

    This code does work for me:

    PHP Code:
    $result = @mysql_query("SELECT * FROM portfolio WHERE category='$cat'");
        if (!
    $result) {
          echo(
    "<p>Error performing query: " mysql_error() . "</p>");
          exit();
        }

        while ( 
    $row mysql_fetch_array($result) ) {
          
    $entryid $row["id"];
          
    $Client $row["Client"];
          
    $Project $row["Project"];
          
    $Industry $row["Industry"];
          
    $Media $row["Media"];
          
    $Solution $row["Solution"];
          
    $Results $row["Results"];

          echo(
    "<p>$Client . $Project . $Industry . $Media . $Solution . $Results."</p>");
        } 
    It outputs line by line, all the information in the database in a nice ugly format. I want free rain with how I format this data. So, essentially, I want the variable to increment by one each time. Not sure how to do this, I just took a shot in the dark:
    With $countvar = 0

    PHP Code:
        while ( $row mysql_fetch_array($result) ) {
          
    $entryid $row["id"];
          
    $Client $row["Client"];
          
    $Project $row["Project"];
          
    $Industry $row["Industry"];
          
    $Media $row["Media"];
          
    $Solution $row["Solution"];
          
    $Results $row["Results"];
          
          
    $Results . echo("$countvar") = $Results 
          $countvar 
    $countvar 1
          
    echo("<p>$Client . $Project . $Industry . $Media . $Solution . $Results."</p>");
          echo(
    "THE MAGIC TEST SAYS $Results1")
        } 
    Obviously, it doesnt work, or else I wouldnt be posting here. Any ideas on the proper way to do this?

  6. #6
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know this is possible. I am just missing something.

    I do a request, and get back multiple rows with multiple fields.

    How do I access not just a single row, but a single field within that row? Shouldnt this work:

    PHP Code:
    echo $result[0]->Date
    Because it doesnt. It doesnt give me an error, it just puts nothing in the end file.
    Last edited by abacusgraphics; Feb 19, 2003 at 13:16.

  7. #7
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dunno if this is right, but if you are saying you want to query a table in the db and only get the value of one field then that is through your sql statement:

    PHP Code:
    $sql mysql_query("SELECT date,time FROM tbl");
    $num mysql_num_rows($sql);

    if (
    $num 0) {

    while (
    $r mysql_fetch_row($sql)) {

    echo 
    $r[0];//this is the date field
    echo $r[1];//this is the time field

    }



  8. #8
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply,

    I am actually going to want to use all the data that my general SQL query pulls, I just want to format it in funky ways. For example, I want the thumbnail image field to be in a layer, but I want each layer to be located differently.

    I already thought I could do a query for the 6 latest entrys, grab their ID numbers, and then do individual queries for each field within each row, but I figured there has to be an easier way, a way to do that data manipulation through php rather then having to do 60 mysql queries.

    How to describe it....So I do a query, asking the MySQL for the latest 6 rows. Each row is gonna pull down with 9-10 different fields. I am gonna use all of this information. I already have it working where it spits it out in a table, its just that my boss, the "designer" here, has some 'brilliant' plan about how we wants it all to work in the end.

    There are gonna be three areas. Along the bottom, a row of 6 thumbnails. To the upper right, a full image of the fullimage field of the first row. To the upper left, the descriptive fields from the first row. But as you click along the thumbnails at the bottom, the image and description change to match that row. It's easy to that all in Javascript, just hide/show layers on click.

    So you can see that I am going to need to place certain things in certain places. For all 6 thumbnail fields, they are each gonna be in their own layer at different locations.

    So what I want to do is either have the general mysql query give me tons of variables like:

    date1, date2, date3, date4, date5, date6
    description1, description2, ......and so on.

    Or, be able to just access the $result variable and pull out just a single field from a single row.

    Is this even possible? It has to be!
    Last edited by abacusgraphics; Feb 19, 2003 at 13:14.

  9. #9
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm. I just stumbled across msql_fetch_row() and msql_fetch_object() and msql_fetch_array(). I think these might help me. Now I just have to toy with them.....

    This is odd (to me at least.)

    PHP Code:
    echo $myrow [0]; 
    Works perfectly fine for me. But if I:

    PHP Code:
    $myrow [0] = $id [0];
    echo 
    $id [0]; 
    Doesnt work. Shouldnt it?
    Last edited by abacusgraphics; Feb 21, 2003 at 13:15.

  10. #10
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by abacusgraphics

    PHP Code:
    $myrow [0] = $id [0];
    echo 
    $id [0]; 
    Doesnt work. Shouldnt it?
    Haha! I got it! I forgot that it should be:
    PHP Code:
    $id [0] = $myrow [0
    woohoo!

  11. #11
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it! Heres the applicable code:

    PHP Code:
    <?php

    if (isset($cat)) {
        
    // Connect to the database server
        
    $dbcnx = @mysql_connect("localhost""XXXXX""YYYYYY");
        if (!
    $dbcnx) {
          echo( 
    "<p>Unable to connect to the " .
                
    "database server at this time.</p>" );
          exit();
        }

        if (! @
    mysql_select_db("abacu9") ) {
          echo( 
    "<p>Unable to locate the  " .
                
    "database at this time.</p>" );
          exit();
        }

    $result = @mysql_query("SELECT * FROM portfolio WHERE category='$cat'");
        if (!
    $result) {
          echo(
    "<p>Error performing query: " mysql_error() . "</p>");
          exit();
        }


    While( 
    $myrow =mysql_fetch_array ($result )) 
    {

    $id ["$count"] = $myrow ["id"];
    $category ["$count"] = $myrow ["category"];
    $client ["$count"] = $myrow ["Client"];
    $project ["$count"] = $myrow ["Project"];
    $industry ["$count"] = $myrow ["Industry"];
    $media ["$count"] = $myrow ["Media"]; 
    $solution ["$count"] = $myrow ["Solution"]; 
    $results ["$count"] = $myrow ["Results"];
    $date ["$count"] = $myrow ["Date"];
    $count $count 1;
    ?>
    Bam! It gives me everything in variables, like I wanted! I guess its kind of good no one posted and told me how, I think I learned a lot mroe doing it myself But I would appreciate if anyone could point out if there is a better or more efficient way to do it.

  12. #12
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can actually simply your loop code a little by letting PHP automatically stick the values on the end of the arrays:
    PHP Code:
    While( $myrow =mysql_fetch_array ($result )) 
    {
      
    $id[] = $myrow["id"];
      
    $category[] = $myrow["category"];
      
    $client[] = $myrow["Client"];
      
    $project[] = $myrow["Project"];
      
    $industry[] = $myrow["Industry"];
      
    $media[] = $myrow["Media"]; 
      
    $solution[] = $myrow["Solution"]; 
      
    $results[] = $myrow["Results"];
      
    $date[] = $myrow["Date"];

    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  13. #13
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks kevin, that works! Shaves a layer of complexity off too, woo-hoo!


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
  •