Announcement

Collapse
No announcement yet.

PHP Questions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Questions

    OK, not exactly vBulletin related, but all the actual PHP forums on the net are crap. Basically, I am building a site for someone from scratch, taking the content of their HTML site and entering it into a MySQL database. I have started learning PHP and can connect to the database, read strings from it and dump them onto a page. What I would like to do is say "Query this, and call the result $nameofresult or whatever so that I can go back and make vB style pages with HTML code that references these.
    The biggest problem is that for the database, I have the URL of an image, but want to call that from the database by PHP and call it $image1url or whatever and then get the PHP to drop it into

    < IMG SRC = "$image1url " > etc

    Help, please?

  • #2
    then just put the path to the image in the database. and call it like you just suggested.

    Comment


    • #3
      Yeah, but thing is, I know heck all about PHP. Can someone please post up how to call a path from a database, then tie it to a variable name, then insert that variable into standard HTML?

      Comment


      • #4
        easy. the path that is in the atabase could be something like /images/img.jpg
        PHP Code:
        $sql "SELECT * FROM image_table'"// queries the database for the image paths
         
        $result mysql_query ($sql);

          if (!
        $result) {echo mysql_error();}  // detects to see if thre is an error and then prints it out.
        while ($row mysql_fetch_array($result)) // gets the table row
                
        {
                     
        $path $row["path"];
                     
                }

        echo 
        "<img src=\"$path\" border=0>"

        Comment


        • #5
          Thanks mate, I appreciate it. Though, watch this space for any further problems I have.

          Comment


          • #6
            Originally posted by scoutt
            easy. the path that is in the atabase could be something like /images/img.jpg
            PHP Code:
            $sql "SELECT * FROM image_table'"// queries the database for the image paths
             
            $result mysql_query ($sql);

              if (!
            $result) {echo mysql_error();}  // detects to see if thre is an error and then prints it out.
            while ($row mysql_fetch_array($result)) // gets the table row
                    
            {
                         
            $path $row["path"];
                         
                    }

            echo 
            "<img src=\"$path\" border=0>"
            Since I'm bored, I'm gonna reformat the code the way I like it...I think MY way is more efficient Just kidding

            PHP Code:
            $result mysql_query("select image_path from the_table") or die(mysql_error());    // get the image_path field from the_table (the name of the table)

            while ($image mysql_fetch_array($result)) {
                echo 
            "<img src=\"$image[image_path]\">";    // print the image

            Actually, your code wouldn'r run correctly, the $path variable gets re-assigned every time it loops through the rows...so only one image would get printed, instead of all of them

            I'm not poking fun, just pointing it out

            Comment


            • #7
              good catch dan, but he didn't say he wanted more than 1 image

              and I just threw that up there as I know there is several ways of doing it. that was a piece I had handy

              Comment


              • #8
                Well, thanks guys for all your help. Basically, I am making a site in PHP/MySQL about a tv series, I have an SQL table called episodes, and in each unique ID I have fields for "epTitle", "epReview", "epRating" and "epImage" etc, what I want to be able to do is create a single PHP page that can accept URL parameters from a search form and build up a list of matching results by filling in the search result data into predesigned HTML tags, looping if necessary until all matching results are shown. Help?

                Comment


                • #9
                  Bah, that's easy Unfortunately, I'm in the midst of doing something else, but I'll get back to you...

                  In the mean time, what version of PHP and what version of MySQL are you running?

                  Comment


                  • #10
                    if it was so easy why couldn't you spend the 2 minutes it took to write it and do it

                    it is all ready done for you. from Dan's post.

                    PHP Code:

                    if($_REQUEST["submit"]){
                    // make sure search word doesn't contain special characters
                    $searchword htmlspecialchars($_REQUEST['searchword']);


                    $result mysql_query("select epTitle, epReview, epRating, epImage from the_table where epTitle = '".%$searchword%."' ") or die(mysql_error());    // get the everything field from the_table (the name of the table) that = the search word

                    while ($row mysql_fetch_array($result)) {
                        echo 
                    "Title = "$row["epTitle"];
                         echo 
                    "Review: "$row["epReview"];
                         echo 
                    "Rated at: "$row["epRating"];
                          echo 
                    "Title = "$row["eptitle"];
                        echo 
                    "<img src=\"".$row["epImage"]."\">";    // print the image
                    }
                    }


                    //then you have your html form here, which I think you can do. 
                    //just make the filed you search from be named searchword
                    // very easy. if you don't know how to do any of this then I would study 
                    // up on php for a bit. 

                    Comment


                    • #11
                      OK, so I can use that PHP above me to gather the info and pass it to another page in the form of a URL referral? Well, what I want is a page that will contain an HTML form, that passes the info to something like "searchresults.php" - which I have mocked up in HTML at this location. - so you can see what I am trying to achieve. Any ideas on how best (easiest) to go about this?

                      Comment


                      • #12
                        so what is hard about that. you have your html page with the form in it

                        <form action="searchresults.php" method=post>
                        <input type="text" name="searchword">
                        <input type="submit" name="submit" value="send">
                        </form>

                        then when it gets subitted it goes to the searchresults page with the last code I showed in my last post.

                        PHP Code:
                        <?
                        if($_REQUEST["submit"]){
                        // make sure search word doesn't contain special characters
                        $searchword htmlspecialchars($_REQUEST['searchword']);
                        ?>
                        <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="65%" id="AutoNumber1">
                        <?

                        $result 
                        mysql_query("select epTitle, epReview, epRating, epImage from the_table where epTitle = '".%$searchword%."' ") or die(mysql_error());    // get the everything field from the_table (the name of the table) that = the search word

                        while ($row mysql_fetch_array($result)) {
                            echo 
                        "Title = "
                             echo 
                        "Review: "
                             echo 
                        "Rated at: "
                              echo 
                        "Title = "$row["eptitle"];
                            echo 
                        // print the image

                        echo<<<myhtml 
                         
                        <tr>
                            <
                        td width="26%"><u><font face="Verdana" size="2"><b>$row["epTitle"]</b></font></u></td>
                            <
                        td width="174%">&nbsp;</td>
                          </
                        tr>
                          <
                        tr>
                            <
                        td width="26%"><font face="Verdana" size="1">$row["epRating"]</font></td>
                            <
                        td width="174%">
                            <
                        font size="1"><img src="$row["epImage"]">    </font></td>
                          </
                        tr>
                          <
                        tr>
                            <
                        td width="26%">&nbsp;</td>
                            <
                        td width="174%"><font size="1">$row["epReview"]</font></td>
                          </
                        tr>
                          <
                        tr>
                            <
                        td width="26%">&nbsp;</td>
                            <
                        td width="174%">&nbsp;</td>
                          </
                        tr>
                          <
                        tr>
                            <
                        td width="26%"><font size="1">Result1</font></td>
                            <
                        td width="174%"><a href="review.php&id=$row["id"]"><font size="1">eplongreview.php&amp;$id=</font></a></td>
                          </
                        tr>
                        myhtml;
                        }

                        echo
                        "</table>";
                        }
                        then you have the review page that gets the id from that link.

                        Comment


                        • #13
                          Tried it with that code, get parse error when attempting to run it, on line 10. I added in the database details as per other simpler pages I have made, can anyone see the particular problem on line 10?

                          PHP Code:
                          <?
                          if($_REQUEST["submit"]){
                          // make sure search word doesn't contain special characters
                          $searchword htmlspecialchars($_REQUEST['searchword']);
                          ?>
                          <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="65%" id="AutoNumber1">
                          <?
                          $db 
                          mysql_connect("localhost","root");
                          mysql_db_select("sg1",$db);
                          $result mysql_query("SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE epTitle = '".%$searchword%."'");   // get the everything field from the_table (the name of the table) that = the search word

                          while ($row mysql_fetch_array($result)) {
                              echo 
                          "Title = "
                               echo 
                          "Review: "
                               echo 
                          "Rated at: "
                                echo 
                          "Title = "$row["eptitle"];
                              echo 
                          // print the image

                          echo<<<myhtml 
                           
                          <tr>
                              <
                          td width="26%"><u><font face="Verdana" size="2"><b>$row["epTitle"]</b></font></u></td>
                              <
                          td width="174%">&nbsp;</td>
                            </
                          tr>
                            <
                          tr>
                              <
                          td width="26%"><font face="Verdana" size="1">$row["epRating"]</font></td>
                              <
                          td width="174%">
                              <
                          font size="1"><img src="$row["epImage"]">    </font></td>
                            </
                          tr>
                            <
                          tr>
                              <
                          td width="26%">&nbsp;</td>
                              <
                          td width="174%"><font size="1">$row["epReview"]</font></td>
                            </
                          tr>
                            <
                          tr>
                              <
                          td width="26%">&nbsp;</td>
                              <
                          td width="174%">&nbsp;</td>
                            </
                          tr>
                            <
                          tr>
                              <
                          td width="26%"><font size="1">Result1</font></td>
                              <
                          td width="174%"><a href="review.php&id=$row["id"]"><font size="1">eplongreview.php&$id=</font></a></td>
                            </
                          tr>
                          myhtml;
                          }

                          echo
                          "</table>";
                          }

                          Comment


                          • #14
                            ahh sorry I left this in. you will have to delete it.
                            echo "Title = ".
                            echo "Review: ".
                            echo "Rated at: ".
                            echo "Title = ". $row["eptitle"];
                            echo // print the image


                            delete all those lines. sorry

                            Comment


                            • #15
                              Nah, that makes no diff, it's line 10 that parses wrong - eg,

                              PHP Code:
                              $result mysql_query("SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE epTitle = '".%$searchword%."'");   // get the everything field from the_table (the name of the table) that = the search word 

                              Comment

                              Loading...
                              Working...
                              X