PHP statement help

Discussion in 'PHP' started by goal442, Jan 22, 2009.

  1. #1
    Hello,
    I'm not very good with PHP, but I need to make a change to almost every page on my site, and I think using a PHP statement would be the fastest way.

    I was hoping some one here can help me figure this out.

    My site is a WordPress blog. I want to do the following.

    I want to add a little bit of more info to each of my posts. I have about 50 different categories, and depending on the category of the post I'm going to add different info.
    Now, I have saved the info that I want to add in different .txt files. (one .txt file for each category).

    I already know how to "get" a .txt file and display it in the location that I want. But I only know how to get the same text file each time.

    The problem that I'm having is, displaying the appropriate .txt file for each category.

    I was thinking about a "IF" or "Switch" statement, but having more then 50 different categories, I would have to write more then 50 lines of "IF" or "Switch" code...For Example

    switch (category)
    {
    case category1:
    code;
    break;
    case category2:
    code;
    break;
    .
    .
    .
    .
    default:
    code;
    }


    So, is there a different way of doing this, without using a "IF" or "Switch" statement?????

    THANK YOU.
     
    goal442, Jan 22, 2009 IP
  2. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    does the text file have the info you want to show based on the catagory?

    If so, you could grab the catagory name through a wordpress command, and call the txt file like so

    $txtcontent = readfilefunction($catagoryname.".txt");

    any time your page loads it whoudl read the catagory name, and call a filename with the same catagory name plus the .txt extension.

    Is that a solution?
     
    darkmessiah, Jan 22, 2009 IP
  3. goal442

    goal442 Peon

    Messages:
    328
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    goal442, Jan 22, 2009 IP
  4. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thats going to be tricky.. the file call will be the full path of the server folders. Personally, I would create a mysql table with this info and call the field(catagory) that way. Heck of a lot better than messing around with the filesystem.
     
    darkmessiah, Jan 22, 2009 IP
  5. goal442

    goal442 Peon

    Messages:
    328
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    darkmessiah
    Thanks again for your quick reply.

    Sorry that I keep asking this questions....but I'm really not very good with PHP and SQL.

    OK, so I would create a table called, lets say, "MoreInfo". The table is going to have two fields, "Category" and "Info". In the "Category" filed I will have all the categories and in the "Info" field all the additional info that goes with the appropriate categories.

    But then, I'm assuming that I would have to use a different statement, correct?
     
    goal442, Jan 22, 2009 IP
  6. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    CREATE TABLE `yourdbname`.`MoreInfo` (
    `id` INT( 80 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `Catagory` TEXT NOT NULL 
    ) ENGINE = MYISAM 
    
    Code (markup):
    now your insert a catagory name along with the data you want to display for that catagory.

    when you need the info for the catagory, you select the catagory name and read in the data it retrieves..

    This is not the most elegant way to do this, but it's good enough.
     
    darkmessiah, Jan 22, 2009 IP
  7. goal442

    goal442 Peon

    Messages:
    328
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have created the database, but do I compare the post category with the SQL field "category" and get the "info" field that belong to that category?
     
    goal442, Jan 23, 2009 IP
  8. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    sql statemnt to get catagory info from db, there was a slight mistake in,the table, you would need 3 fields.. and ID , Catagor name, and Content

    you insert something like this

    "INSERT INTO `MoreInfo` VALUES('', '{$catagoryname}', '{$content}');"


    "SELECT * FROM `MoreInfo` WHERE `catagory` = '{$wpcatagorname}' LIMIT 1"

    $row = fetch_row($sqlqueryresult);

    $contenttobeshownonsite = $row['content'];
     
    darkmessiah, Jan 23, 2009 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    If you're on a Linux server, sed, which is most likely already installed, would probably be faster.
     
    joebert, Jan 24, 2009 IP
  10. goal442

    goal442 Peon

    Messages:
    328
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks for all the replys,

    I have created my table "wp_info" with the following fields:
    ID_info (PK)
    cat_id
    fullname_value
    founded_value

    Now each post on my site is also assigned to a category, and each category has it's own cat_id.

    I want to compare the cat_id of each post to the cat_id in the SQL table "wp_info" and display the values of the 2 fields "fullname_value" and "founded_value".

    I'm just getting confused with the PHP code. I'm not sure how to compare the cat_id from the post with the cat_id in the SQL table and also how to display the fullname_value and founded_value for each category.

    Any help will be greatly appreciated.
     
    goal442, Jan 27, 2009 IP
  11. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #11
    $cat_id = wordpressfuntiontogetcatid();

    do a sql SELECT query on wp_info, SELECT from wp_info WHERE cat_id = $cat_id

    do a fetch row off the returned result from the query.

    the result from fetched row command will be an array.. you can access each field of the table like so

    $row['fullname_value']
    $row['founded_value']
     
    darkmessiah, Jan 27, 2009 IP