SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Small Script Problem - Newbie Needs A Bit of Guidance

    Hi Guys


    Heres my coding it is for a simple search function for my site.

    http://www.richard-howells.co.uk/final_project/find.php

    PHP Code:
    <html>
    <
    head>
    <
    title>Goldcast Computers</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </
    head>

    <
    body>

    <
    form method=post action="search.php"

    Search For: 
      <
    pProducts 
        <
    input type=text name=search size=25 maxlength=25>
      <
    p
    <
    input type=submit
    </
    form>      
    </
    body>
    </
    html

    http://www.richard-howells.co.uk/fin...ect/search.php


    PHP Code:
    <html>
    <head>
    <title>Goldcast Computers</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php
    include ('includes/misc.inc');
    //You should now this but be sure to connect to your database or this is useless. :/ 
    if ($search == ""
    {
    $search '%';} 

    // $user and $passwd are the two column names I used.  Change this to fit your database 
    $result mysql_query ("SELECT * FROM products WHERE product_type_description LIKE '%$search%' "); 
                                                                      
    if (
    $row mysql_fetch_array($result)) { 

    do { 
      PRINT 
    "<b>User Name:     </b>     "
      print 
    $row["product_type_description"]; 
      print (
    " "); 
      print (
    "<br>"); 
      PRINT 
    "<b>Passwd:     </b>     "
      print 
    $row["product_name"]; 
      print (
    "<p>"); 
      PRINT 
    "<b>FirstName:     </b>     "
      print 
    $row["product_description"]; 
      print (
    "<p>"); 
    } while(
    $row mysql_fetch_array($result)); 
    } else {print 
    "Sorry, no records were found!";}   

    //The end result prints the username / passwd / firstname / lastname / ID / email 
    //you will have to change these fields also to fit your needs 
    ?> 
    </body>
    </html>

    ERROR MESSAGE:
    Parse error: parse error, unexpected T_IF in /home/richard/public_html/final_project/search.php on line 11
    I guess this is something simple any suggesdtion on how to make this work or to improve on slightly, greatley appreciated.


    Best Regards
    Richard Howells

  2. #2
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    Certainly the error is at the end of 'includes/misc.inc'
    but will detected only when coming back on your if
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi its sorted,

    It works fine now thankyou!

    As this is for a computer supplies store, can you make any suggestions on how to develop this simple search function, i.e add in search for price ranges etc etc.

    At presant it will only serach for words in the product_type_description row of the 'products' database. How can i do it so that it will search the 'product_name' row also?


    Any help or suggestions etc greatley appreciated.

    Cheers
    Best Wishes
    Richard

  4. #4
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do:
    PHP Code:
    $sname 1// for search on name, 0 for search on desc
    $result mysql_query ("SELECT * FROM products WHERE ".
     (
    $sname ?  'product_name' 'product_type_description')." LIKE '%$search%' "); 
    You need to get this boolean $sname in the same time you get the search value from your user.
    Best
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, again it works fine.


    Many Thanks

    Okay what I have tried to do is put all the relveant information I want stored into a table.

    I wan it to work like my viewproducts.php page.

    This is my code as I have it, I know its just about correct im missing ., ", ' etc etc in the code, as Im fairly new im unsure as to where and how.

    PHP Code:
    <?php
    include('includes/misc.inc');

    //You should now this but be sure to connect to your database or this is useless. :/ 
    if ($search == ""
    {
    $search '%';} 

    // $user and $passwd are the two column names I used.  Change this to fit your database 
    $sname 1// for search on name, 0 for search on desc 
    $result mysql_query ("SELECT * FROM products WHERE "
    (
    $sname ?  'product_name' 'product_type_description')." LIKE '%$search%' "); 
                                                                      
    if (
    $row mysql_fetch_array($result)) { 

    do { 
      PRINT 
    "<table cellpadding='0' cellspacing='2' border='0' width='524'>";
      PRINT 
    "<tr><td class='productsdisplayedimageborder'><img src="images/thumbnail_images$row['product_thumbnail_image']"></td>";
      PRINT 
    "<td class='productsdisplayedbackground' valign="top"> <a href="viewitem.php?product_item$row['product_id']; $row['product_name'];</a> <br> <span class='productsdisplayeddescription'$row['product_description']; </span> </td>"; 
      print "
    <td class='productsdisplayedprice' width="62"$prod_rows['product_price'];</td>"; 
    } while(
    $row = mysql_fetch_array($result)); 
      PRINT "
    </table";

    else {
    print "
    Sorryno records were found!";
    }   


    ?>
    Please advise, cheers

    Best Wishes

    Richard

  6. #6
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you want it row by row, I think it's mysql_fetch_row($result) you should use.
    If I'm not too much asleep,
    mysql_fetch_array($result) catches all rows in once so result is a bidimensional array.
    Go on, boy.
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh,

    My code is parsing, there is obviously a couple of problems with it, things not quite in the correct places, could you please take a look and advise of possible changes.

    Regards

    Richard

    PHP Code:
    <?php
    include('includes/misc.inc');

    //You should now this but be sure to connect to your database or this is useless. :/ 
    if ($search == ""
    {
    $search '%';} 

    // $user and $passwd are the two column names I used.  Change this to fit your database 
    $sname 1// for search on name, 0 for search on desc 
    $result mysql_query ("SELECT * FROM products WHERE "
    (
    $sname ?  'product_name' 'product_type_description')." LIKE '%$search%' "); 
                                                                      
    if (
    $row mysql_fetch_row($result)) { 

    do { 
      PRINT 
    "<table cellpadding='0' cellspacing='2' border='0' width='524'>";
      PRINT 
    "<tr><td class='productsdisplayedimageborder'><img src="images/thumbnail_images$row['product_thumbnail_image']"></td>";
      PRINT 
    "<td class='productsdisplayedbackground' valign="top"> <a href="viewitem.php?product_item$row['product_id']; $row['product_name'];</a> <br> <span class='productsdisplayeddescription'$row['product_description']; </span> </td>"; 
      print "
    <td class='productsdisplayedprice' width="62"$prod_rows['product_price'];</td>"; 
    } while(
    $row = mysql_fetch_array($result)); 
      PRINT "
    </table";

    else {
    print "
    Sorryno records were found!";
    }   


    ?>

  8. #8
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ooops I was wrong

    Withdraw my last comment on mysql_fetch_array()
    You're definitively right.
    And I'm definitively too tired.
    Excuse me.
    Bye.
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Small teething errors with code

    Hello this is a continuing problem, a small one I believe. As you can see I have had some help with this for which I am very greatful.

    As a newbie I am a little in experienced at fault finding.

    Has anyone got any suggestions on how to make this code work?

    Many THanks

    Richard

    PHP Code:
    <html>
    <head>
    <title>Goldcast Computers</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php
    include('includes/misc.inc');

    //You should now this but be sure to connect to your database or this is useless. :/ 
    if ($search == ""
    {
    $search '%';} 

    // $user and $passwd are the two column names I used.  Change this to fit your database 
    $sname 1// for search on name, 0 for search on desc 
    $result mysql_query ("SELECT * FROM products WHERE "
    (
    $sname ?  'product_name' 'product_type_description')." LIKE '%$search%' "); 
                                                                      
    if (
    $row mysql_fetch_array($result)) { 

    do { 
      PRINT 
    "<table cellpadding='0' cellspacing='2' border='0' width='524'>";
      PRINT 
    "<tr><td class='productsdisplayedimageborder'><img src="images/thumbnail_images$row['product_thumbnail_image']"></td>";
      PRINT 
    "<td class='productsdisplayedbackground' valign="top"> <a href="viewitem.php?product_item$row['product_id']; $row['product_name'];</a> <br> <span class='productsdisplayeddescription'$row['product_description']; </span> </td>"; 
      print "
    <td class='productsdisplayedprice' width="62"$prod_rows['product_price'];</td>"; 
    } while(
    $row = mysql_fetch_array($result)); 
      PRINT "
    </table";

    else {
    print "
    Sorryno records were found!";
    }   


    ?> 
    </body>
    </html>

  10. #10
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Take care of the strings !

    PHP Code:
    <?php
    include('includes/misc.inc');

    //You should now this but be sure to connect to your database or this is useless. :/
    if ($search == "")
    {
    $search '%';}

    // $user and $passwd are the two column names I used.  Change this to fit your database
    $sname 1// for search on name, 0 for search on desc
    $result mysql_query ("SELECT * FROM products WHERE ".
    (
    $sname ?  'product_name' 'product_type_description')." LIKE '%$search%' ")
    or die(
    'ERROR :'.sql_error()); // just to check it's OK
    $OK "</table><BR>Sorry, no records were found!";
    // better to have an only $row fetching, you missed last correction
    echo "<table cellpadding='0' cellspacing='2' border='0' width='524'>";
    while (
    $row mysql_fetch_array($result)) {
      
    $OK "</table";
      echo 
    "<tr><td class='productsdisplayedimageborder'><img src='images/thumbnail_images/{$row['product_thumbnail_image']}'></td>";
      echo 
    "<td class='productsdisplayedbackground' valign='top'> <a href='viewitem.php?product_item=".urlencode($row['product_id'])."'>".
      
    htmlentities($row['product_name'])."</a> <br> <span class='productsdisplayeddescription'>.
      htmlentities(
    $row['product_description'])." </span> </td>";
      echo "
    <td class='productsdisplayedprice' width='62'>.
      
    htmlentities($row['product_price']). "</td>";
    }
    echo 
    $OK;
    ?>
    I have sanitized all your strings : best "" around and ' inside so the variable can expand (best is variable to expand within brackets)
    + urlencode for get parameter (viewitem);
    + htmlentities for body's content.
    Take care of start/end of strings.
    Enjoy
    +
    Sure not tested out . Perhaps you shoud do it
    Last edited by toggg; Jan 29, 2005 at 08:01. Reason: a small ' inverted, OK now
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  11. #11
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    teething problems

    Great Many Thanks

    Needed a couple of minor changes, but manged to fix them.

    Two small problems are that:

    1. Style sheets are not working.
    2. Search not quite working e.g

    if i put search for 'memory', i only get results from 'portable memory' there should be results from 'pc memory' aslo. and another example a search for 'Graphics Card' returns no results.

    ahhhhhhhhhhhhhhhhhhhhhhhhhhhh

    Regards
    Richard

    PHP Code:
    <?php 
    include('includes/misc.inc'); 

    //You should now this but be sure to connect to your database or this is useless. :/ 
    if ($search == ""
    {
    $search '%';} 

    // $user and $passwd are the two column names I used.  Change this to fit your database 
    $sname 1// for search on name, 0 for search on desc 
    $result mysql_query ("SELECT * FROM products WHERE "
    (
    $sname ?  'product_name' 'product_type_description')." LIKE '%$search%' "
    or die(
    'ERROR :'.sql_error()); // just to check it's OK 
    $OK "</table><BR>Sorry, no records were found!"
    // better to have an only $row fetching, you missed last correction 
    echo "<table cellpadding='0' cellspacing='2' border='0' width='524'>"
    while (
    $row mysql_fetch_array($result)) { 
      
    $OK "</table"
      echo 
    "<tr><td class='productsdisplayedimageborder'><img src='images/thumbnail_images/{$row['product_thumbnail_image']}'></td>"
      echo 
    "<td class='productsdisplayedbackground' valign='top'> <a href='viewitem.php?product_item=".urlencode($row['product_id'])."'>"
      
    htmlentities($row['product_name'])."</a> <br> <span class='productsdisplayeddescription'>"
      
    htmlentities($row['product_description'])." </span> </td>"
      echo 
    "<td class='productsdisplayedprice' width='62'>"
      
    htmlentities($row['product_price']). "</td>"

    echo 
    $OK
    ?>

  12. #12
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    not looking at 'product_type_description' within the database table called 'products'

    Basically it is not looking at 'product_type_description' within the database table called 'products'.


    ahhhhhhhhhhhhhhhhhhhhhhhh

    I have tried to produce the answer but to no success.

    Best Regards

    Richard :-)

  13. #13
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I said to you it's working with this variable $sname.
    if its value = 1, that will be on name
    if its value = 0, that will be on description.
    Then I don't know what for it is.
    E.g. you could put a checkbox for this $sname variable meaning on name if checked.
    I give up for today, but try to read and understand the code. You could come clear by yourself.
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  14. #14
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    teesside
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thankyou so much

    regards
    richard


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
  •