What To Do to Populate Pages with PHP

Aug 31st, 2015

A common question:

I have a database with all the images I need to fill the page I’m connected to the database. Now the thing I don’t know how to do is to call the images through PHP and have them show up on the page. I know I will need a for loop to cycle through all the images and give them the proper tags but how do I make the initial call to the database? I’m using PHP5 and MySQL database with PHPMyAdmin.

Now I know how to do this through dreamweaver but the code it generates is so messy and ugly that I’m not going to do it that way.

Be as long winded as possible with answers and if you can point me towards video tutorials or any thing that would be great as well.

Thanks


Answer: This is quite a popular question. Web hosting is the heart of your web presence and can mean the life or death to your business.

Creating your site won’t be difficult if you want to use a a powerful content management system like WordPress. WordPress has huge selection of themes and plugins that you can use in creating your sites so that you won’t have to code your site manually. It can also be easily installed via automatic installer like Fantastico and QuickInstall that can be found in your cPanel.

This one-click installer like Fantastico and QuickInstall can be found in your cPanel. You need to specify the software you want to install, fill up some details and specify the domain or subdomain you want to use before hitting the ‘Install Now’ button. Your WordPress site is ready when you receive a successful installation notice.

  • SCRN – a single responsive portfolio wordpress template. Perfect to promote your work or your business. Is compatible with all modern mobile devices. Other than the minimalistic single page design, it also has a blog.

    View Live Demo | More Info

  • GridStack – unique and modern portfolio theme for agencies, artists and freelancers

    View Live Demo | More Info

Before we give you a definitive answer, these are seven factors to look for in a good web hosting. An excellent hosting:

  • has easy-to-use Site Builder,
  • has Fantastico and QuickInstall to quickly install apps like WordPress, Joomla, Drupal, OSCommerce, ZenCart and more

Answering your question, more than 80% of experienced Webmasters recommend to try Hostgator. If you are building your first website and needs a hosting plan, experts usually highly recommend going with Hostgator because they are the best web hosting around.

What if you find out that Hostgator sucks after you signed up with them? HG has an amazingly long, 45 days money back guarantee so you have ample time to test them out. They are rated A+ by Better Business Bureau which shows their commitment to customer satisfaction. You also do not have to pay for the first month. You can just try their fully functional hosting service – you pay only $0.01 (you need to use the special coupon ). Do you know any other hosting company that can give you that kind of assurance? Anybody can grab an account from Hostgator for almost FREE.

How to Get an Unlimited Hosting Plan for Only 1 Cent

Click the coupon below try Hostgator cPanel hosting for almost free. If you already know that Hostgator is what you want, you can even save 25% off the normal price with the Hostgator coupons 2015 below.

Who Should Not Try Hostgator

To be honest, no web hosting service is perfect. Drawback of Hostgator includes:

  1. No free domain name – but you can easily get a domain name from the best domain registrar such as Godaddy or Namecheap for $10 or less. Would you agree that a website is not complete without a domain name?
  2. You need to pay full price after your first invoice – well, they need to make money too and all the support and great service do come with a cost

If you are still not sure if HG is right for you, or you have specific query about anything at all, try the Live Chat at HG. They are fast and knowledgeable. Just shoot them some questions before you decide.

  1. Colinc
    Aug 7th, 2013 at 02:52
    Quote | #1

    First you need to have a file in a directory under the root directory with setup information for the database and anything else which might be sensitive. This is to protect your self against anyone gaining access by seeing any source code. Ideally using definitions of constants.
    define ("DB_HOST","localhost");
    define ("DB_NAME","yourdatabase");
    define ("DB_USER","youruser");
    define ("DB_PASSWORD","userpassword");
    The user must not be your full admin rights user, it must not be root as this name is known worldwide. Do not give the user the grant ( including create or drop privileges) or an attacker can obviously do massive damage that way. Set the data connection in this file.
    $conn = mysql_connect (DB_HOST,DB_USER,DB_PASSWORD);
    Do not try to actually store the images, videos or similar in the data. Store them in directories and place the file names in the database. Set the directory path from the include file again.
    define ("IMAGE_DIRECTORY","images/");
    include a field in the tables for obsolete, or similar, set to 1 for on, 0 for off, gives you a way to quickly turn off or on any image you want to prevent from viewing for any reason. It also removes the need to delete any data, which should be avoided to maintain data integrity.
    $data_table = "images"
    Your display routine should be something like this, which makes the automated building of queries simple.
    $image_query = "select * from ".DB_DATABASE.".".$data_table." where obsolete = ‘0’ order by image_name";
    $image_res = mysql_query ($image_query,$conn);
    if (mysql_num_rows($image_res) > 0) {
    while ($image_rec = mysql_fetch_array ($image_res)) {
    $image_file = IMAGE_DIRECTORY.$image_rec[image_name];
    echo "…
    //include your layout information and the image source information
    }
    }else{
    echo (<h2>No images are yet stored</h2>;
    }
    This last being the fallback for an empty table or all images being set obsolete.
    The routine above can be built as a function, possibly in the same file as the definitions, then by using
    function get_data($table_name){
    global $conn;

    You can call the function from anywhere in the site. Clever data structures would allow you to pass and return enough for ANY type of data using the same function, hence the use of a variable for the table name. Just include the setup page in every page with data required.

  2. Poki
    Aug 7th, 2013 at 03:06
    Quote | #2

    http://www.tizag.com/mysqlTutorial/mysqlconnection.php

    http://w3schools.com/php/php_mysql_connect.asp

Comments are closed.