SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL Driven Directory

    Hi all,
    I have a breeder directory on my website that I would like to change. Basically, everything is hardcoded in HTML right now. What I would like to do is to store the info in a database and then use embedded PHP to recall the data and input it into the HTML template.

    Here is a link to the directory as it is now.
    http://www.pets-on-the-net.com/breeders.html

    Can anyone point me to a tutorial that would walk me through step-by-step?
    It seems that most of the tutorials that I have found are either to post information to the database or to retrieve it through a basic search. I have yet to come across an understandable tutorial on how to embed PHP.

    Thanks,
    Candice
    Pets On The Net - A forum for pet owners

  2. #2
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm no pro, but I think it's done like

    <?php
    your php code goes here
    ?>
    edit:

    so if you want to put php in a table it would be like

    <table>
    <tr>
    <td>
    <?php
    echo "hello world (to coin a phrase)";
    ?>
    </td>
    </tr>
    </table>

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    ecuador
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    start here

    And ask if you come any issues while coding it

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I guess I didn't explain myself very well.

    My HTML code looks like this...
    Code:
    <p><table class="genmed">
    <tr><td><font size="2" color="#DD6900"><b>Name:</b></font> Edward Roberts</td></tr>
    And instead of hardcoding everything, I would like to have something like...
    Code:
    <p><table class="genmed">
    <tr><td><font size="2" color="#DD6900"><b>Name:</b></font>PHP code to pull name from database here</td></tr>
    Does that make sense? That example is from this page...
    http://www.pets-on-the-net.com/direc...d/Florida.html
    Pets On The Net - A forum for pet owners

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    ecuador
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As stated before
    Start Here

    Mysql and PHP

    try to code some and come back if you have errors or whatever

  6. #6
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in that case, you can probably do it like

    Code:
    <p><table class="genmed">
    <tr><td><font size="2" color="#DD6900"><b>Name:</b></font><?php include ("phpscript.php"); ?></td></tr>
    where it says phpscript.php, change that to your scripts file name but leave everything else... that might work

    you might run into problems if the php script is repeating things that are already in your source code like your doctype, meta tags, <head></head><html> and so on so you'll have to take care of that...

  7. #7
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could also use:

    php requrie ("filename")
    php include_once ("filename")
    php require_once ("filename")

    up to you and your needs....check the manual for descriptions of how these statements work

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you can see from my website, there are several different categories of breeders, based on species. Would it be easier for me to make a mass table (entitled breeders) and store all of the information in there? Or would it be better to seperate each species into its own table (eg. birds, cats, etc.)?
    Pets On The Net - A forum for pet owners

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I am doing well so far.

    I have a (hopefully) simple question. When breeders apply for the directory, some of the information is optional, such as phone number, website, etc. If you visit this page you will see where the problem lies. Is there a way to make "Phone:" not appear if no phone number is supplied?
    Pets On The Net - A forum for pet owners

  10. #10
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    think about what you are storing in the table.

    Generally, you need a table for each real-world object that you are storing data for.

    If you are storing information about the different breeds and or species, (like akc standards, origin, sires, list of champions or something) I would suggest a seperate table.

    If your only reference to the breeds or species is who is breeding them, then you should be fine with just the one table. This reference is more of an attribute of the breeder than the breed (like an address or e-mail would be).

    hope this helps
    there are no stupid questions

  11. #11
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Candycane
    Okay, I am doing well so far.

    I have a (hopefully) simple question. When breeders apply for the directory, some of the information is optional, such as phone number, website, etc. If you visit this page you will see where the problem lies. Is there a way to make "Phone:" not appear if no phone number is supplied?
    Just check whether or not that variable (pulled from the DB) is empty. Then, opt out of displaying it:

    PHP Code:
     if (!empty($phone))
         echo 
    "<td>Phone: $phone</td>"
    My name is Steve, and I'm a super-villian.

  12. #12
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Candycane
    Okay, I am doing well so far.

    I have a (hopefully) simple question. When breeders apply for the directory, some of the information is optional, such as phone number, website, etc. If you visit this page you will see where the problem lies. Is there a way to make "Phone:" not appear if no phone number is supplied?
    yes, with an if else statement. But the html code that displays it will have to be written on-the-fly by your php script.
    there are no stupid questions

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, if my code looks like this
    Code:
    <tr><td><font size="2" color="#DD6900"><b>Phone:</b></font> <?php echo $row->Phone1; ?></td></tr>
    What would it look like with the if statement?

    Thanks for your help Got2BMe and lieut_data. I have just finished a few of the tutorials and I now have a functional form to add the information to the database.
    Pets On The Net - A forum for pet owners

  14. #14
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, how do I make fields on my form show up as NULL in the database when they are not filled in? For example, I have three fields for email addresses but most breeders only use one address...how do I make the second and third addresses NULL?
    Pets On The Net - A forum for pet owners

  15. #15
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like I said, I'm no pro at php... anything else for that matter but I'm learning.

    Anyway, others (I am positive) could give you better codesnipit than I but this is my rudimentary way of doing it.

    PHP Code:
    if ($row->phone
    {
       echo 
    "<tr><td>";
       echo 
    "Phone: ";
       echo 
    stripslashes($row->phone);
       echo 
    "</td></tr>";

    You should be doing something to filter data going into and coming from your database, many different ways to do this but given your understanding (mine isn't much more)...for basics, check the functions "addslashes", "htmlspecialchars", "trim" and "stripslashes" for strings and “doubleval” for numeric data. There are some very good articles on this site and elsewhere about "mysql injection" you should read.
    there are no stupid questions

  16. #16
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want it to store the value "NULL" I think you have to set the default to "NULL".

    I think you may be getting confused about "not null" which means the field can't be empty. so if you want it to show "NULL", then you must set the field to "not null" with a default value of "NULL".... unless I'm confused which I may be because I don't know if a default value that is string data can be used for a numeric data field...
    there are no stupid questions

  17. #17
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again, Got2BMe.

    I will try that code later. In my database, the fields are set to NULL as default. However, I have created an html form to input data and that is causing the problem. If I leave parts of the form blank, they are not inputting as null, despite the field having null as the default.

    For example, this code is in my form.
    PHP Code:
    $Email1 $_POST['Email1'];
    $Email2 $_POST['Email2'];
    $Email3 $_POST['Email3']; 
    Most breeders have one email addresses but usually don't have 2 or 3. All of my fields in the database are set to null. But, when I use the form to input the info, the fields are reset to be blank...but not null. I'm guessing that I need to add something to the form that I am using.
    Pets On The Net - A forum for pet owners

  18. #18
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, change the fields to "NOT NULL" and type in the default value of "NULL".

    The "default" value in a field set to "NOT NULL" is what you want stored in the field if no data is supplied for that field from your user. If the field is set to "NULL", that means that the field "CAN" be left empty and no "default" value is required.

    null = blank
    if no data is input

    not null = default value
    if no data is input
    there are no stupid questions

  19. #19
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, again, for your help.
    I tried setting the fields to not null with the default value of NULL and I get,
    "#1067 - Invalid default value for 'Username'"
    Pets On The Net - A forum for pet owners

  20. #20
    SitePoint Member Got2bMe's Avatar
    Join Date
    Jan 2004
    Location
    Florida
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can only guess that you are using "Username" as the "primary key" for your table.

    The "primary key" for your table should be set to "not null" as well as any other fields that should have "unique" values.

    easy fix... change the "Username" feild to "not null" (no default value). How many members do you want with the name "Null"?

    Think about how you want to let people sign up, do you want anonymous sign-ups? If so, don't use "Username" as the "primarykey" and don't set it "unique". This will allow you to give anonymous users the default "Name" of "NULL" if thats what you want.
    there are no stupid questions

  21. #21
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you again. I have now figured it out and I am entering the data into the database and creating pages. The way that the form is set up now, breeders apply, and then the information is sent to me via email and I hard code it into the pages. Now, given the new database, can I still have some control over entries by being able to approve them before they appear on the website? Otherwise, I would just enter the information from the email...
    Pets On The Net - A forum for pet owners

  22. #22
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I just ran into a small problem.

    Some of the breeders breed more than one species.
    When I use this statement in my pages,
    Code:
    $sql = "select * from breeders WHERE Breeding = 'Bird' AND State = 'Illinois' ORDER BY Member, Referrals ASC";
    the breeders who breed birds, rabbits, and dogs (for example) do not appear. How do I fix this?
    Pets On The Net - A forum for pet owners

  23. #23
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?
    Pets On The Net - A forum for pet owners


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
  •