Announcement

Collapse
No announcement yet.

Load an image in a table

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

  • Load an image in a table

    I have a page that has a table where the results of a query to the database are posted

    I want to add an extra column to display an image related to one of the other fields.

    I know nothing about php but wonder if it is possible to avoid adding it to the database and instead use inside <td></td>.

    Something like if $Chain ="Accor" load Accor.jpg

    if it is possible what would the code be, please?

    EDIT

    Restating my problem: how do I add an image field to a database table? Or is it possible to do it in the html/php page after receiving the results of the query?
    Last edited by qim; 12-01-2015, 02:34 AM.

  • #2
    Hi qim,
    you don't store images in a database (well, you can, but it's not advisable)
    Instead, you store the name and / or location of your image in the database and add the image itself to a folder in your website as normal, VARCHAR 255 data type would be fine for storing images.
    You can run the following query to add an additional database column (replace tablename with the name of your database table -

    Code:
    ALTER TABLE tablename ADD image VARCHAR(255);
    You will now have an additional column called 'image' in your database. The contents of this column could be -
    "assets/images/myimage.jpg" if you are storing the full path, or just "myimage.jpg"

    hope this helps
    Last edited by Reiss; 12-01-2015, 08:34 AM.

    Comment


    • #3
      Hi Reiss

      I'm new to this and it's all very confusing!

      let's take an example:

      1 line of my database reads


      Accor,Portugal,Oeiras,n,n,y

      That is

      $chain,$country,$City, $Top,$Medium,$Low

      What you are suggesting is to add a new column to the table, call it IMG and then what do I put in it?

      can I just give it a code, say AC, for the above example and then get the php to work out that AC represents mywebsite//images/Accor.jpg ?

      Thanks

      Comment


      • #4
        This Might Help...

        en.wikipedia.org/wiki/Base64
        Last edited by Structure; 12-01-2015, 11:33 PM. Reason: because i can

        Comment


        • #5
          How?...........

          Comment


          • #6
            Ignore structure's reply, he's hinting that you can base 64 encode your image and store it in the database, not the recommended solution.

            In your example, you would end up with an additional column like so -
            Accor,Portugal,Oeiras,n,n,y, Accor.jpg

            you would access your database entry for the image like so (depending on your query of course) -

            $chain,$country,$City, $Top,$Medium,$Low, $img

            in your front end, you could do something like -

            Code:
            images/<?php echo $img; ?>
            if you can show us a little more code and your database query, I can help you further.
            Cheers

            Comment


            • #7
              Hi Reiss

              I've managed to progress a bit, but just a bit. I got some code that might solve the problem without an extra column in the database (5000+ records)

              Several pages will have a Search box to input the query; the output goes to a separate page.

              The query is about which hotel chains have hotels in a particular city. You enter the name of a city and you may get 1 Chain, or you might get 10.

              In the output there will be one row per chain. see the skeleton of the pages here: http://pintotours.net/TEMP/SPACE/test10.html Enter "Brussels" or "Lisbon"

              I need to place an image at the beginning of each row (a clickable logo link). I thought it could be done within the table <td></td> with something like "if $Chain ="Accor" load Accor.jpg"

              Today I got some code (a function: function getImage($chain='NULL') )

              that you can see in the partial code that I am pasting below, but I can't get it to work!

              Code:
              .......
              $searchq = filter_var("%{$_POST['keyword']}%", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); // Sanitize the string
              
              $sql = "SELECT Chain, Country, City, Top, Medium, Low FROM Chains WHERE Country LIKE ? OR City LIKE ?"; // Your query string
              
              
              $prepare = $mysqli->prepare($sql); // Prepare your query string
              $prepare->bind_param('ss', $searchq, $searchq); // Bind the placeholders to your search variables
              // s = string | i = integer | d = double | b = blob
              $prepare->execute(); // Execute the prepared statement
              If you have a solution...

              Many thanks
              Last edited by qim; 12-06-2015, 05:21 PM.

              Comment


              • #8
                first, add the image column to your database table as I showed you earlier. Then include it in your query -
                Code:
                $sql = "SELECT Chain, Country, City, Top, Medium, Low, img FROM Chains WHERE Country LIKE ? OR City LIKE ?"; // Your query string
                Code:
                $prepare->bind_result($chain, $country, $city, $top, $medium, $low, $img); // Append variables to the columns you specified
                forget your 'getImage' function and just include the image in your loop like so -

                Code:
                <?php 
                while($prepare->fetch()) { 
                ?>
                            <tr>
                                <td> <a href="somelink"><img src="images/<?php echo $img; ?>" alt="<?php echo $chain; ?>" /></a></td>
                                <td> <?php echo $chain; ?></td>
                                <td> <?php echo $country; ?></td>
                                <td> <?php echo $city; ?></td>
                                <td> <?php echo $top; ?></td>
                                <td> <?php echo $medium; ?></td>
                                <td> <?php echo $low; ?></td>
                            </tr>
                <?php } ?>
                Just make sure that you have your images already in the images folder (or whatever path you choose) and that the correct name is stored in the database table.

                Comment


                • #9
                  Thank you Reiss

                  It's late here and my head is tired. I will have a go at this tomorrow morning (CET)

                  I hope you're going to be around should I (of course, I will!) get into trouble!

                  Many thanks

                  PS

                  Just one question: I create a new column and call it img. Can I not place a code in the field' Say the $Chain is Accor, Can I put in the $img field AC ?

                  and then somewhow connect AC to the location where the img is?

                  If not, what do I have to put in img field' The full path to the image or just the name of the img file?
                  Last edited by qim; 12-01-2015, 04:12 PM.

                  Comment


                  • #10
                    you just put the image name in the database column, full path or the name, it's up to you. It's only a bit of text the same as all your other content that needs echoing out in your front end, try not to over complicate things
                    I usually put the image name only in case I need to re-organise my folder structure at some point.

                    Comment


                    • #11
                      This Might Help...

                      Ignore structure's reply, he's hinting that you can base 64 encode your image and store it in the database, not the recommended solution.
                      If you do not like this solution you are entitled to your opinion.

                      It is a Solution, and it will work. Simple and easy to do.
                      Last edited by Structure; 12-01-2015, 07:32 PM.

                      Comment


                      • #12
                        Structure, posting a link to a seemingly random page (from the OP's point of view) does not constitute a helpful reply.
                        You seem to have a habit of doing this and it really isn't helpful without some form of explanation to accompany the URL.
                        Your solution is also bad practice, if you are going to give adice, make sure that it is at least accurate and recommended, otherwise, you are just advertising bad practices not only to our members, but other visitors who have arrived from SERP's.

                        Comment


                        • #13
                          This Might Help...

                          posting a link to a seemingly random page
                          You seem to have a habit of doing this and it really isn't helpful without some form of explanation
                          This is fair. Understood.

                          Your solution is also bad practice
                          This is your opinion. IMO you are incorrect.

                          make sure that it is... recommended
                          Maybe you fail to understand i am giving a solution that might help get the job done.
                          The title of the posts are THIS MIGHT HELP... for a reason.

                          In this case not only could you use the BASE64 for your image but you can also learn about what base64 is and how or why you could use it for other projects. As far as accuracy it is completely accurate. You can base64 a lot of files most commonly image files.

                          If i can deliver an easy and efficient solution that can be understood and implemented vs spewing bunches of lines of code and having to give you the code without you understanding and spending all day trying to explain it. I think that is more helpful.
                          Last edited by Structure; 12-01-2015, 11:30 PM.

                          Comment


                          • #14
                            Originally posted by Structure View Post


                            In this case not only could you use the BASE64 for your image but you can also learn about what base64 is and how or why you could use it for other .
                            Hi Structure

                            Thank you very much for your useful advice, regarding BASE64.

                            I have left some links to my site in this thread. You would not have made those comments without having had a look at the site, and so I am surprised that you missed the extensive use of BASE64 throughout.

                            As you noticed, Reiss is very kindly helping me with my current problem, and as they say "too many cooks spoil the broth"

                            Regards

                            Comment


                            • #15
                              Hi Reiss

                              It works, and I can now see the end of the tunnel!

                              May I ask for help on another little matter?

                              You can see the current situation in http://pintotours.net/TEMP/SPACE/input.html

                              If you enter "Brussels" to get more than one result, you will see that not only I get the data requested, but also headings for each row!

                              Now, I know I can get rid of the headings in more than one way, and so my question is: can we get rid of the headings EXCEPT for the top one?

                              If not in the php, maybe in javascript?

                              Of course, I can (and tried) simply to put them there in HTML, but it will not work (that I know...) once you start narrowing the screen, so it will be useless for tablets.

                              There will be a different table version for mobiles, later.

                              Many thanks
                              Last edited by qim; 12-02-2015, 04:34 AM.

                              Comment

                              Working...
                              X