#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Th tag on demand


    hi everyone!

    I was wondering if there's a possibility of using html th tag on demand.For example I have the following tag

    <th>name</th>

    and i want to have another one

    <th>address</th>

    but the thing is that i want the second one to appear only if the php code which comes after these statements returns data for it.The php code always returns data for the first one but only sometimes for the second one.Is there a way to achieve that?
  2. #2
  3. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    312
    yes.

    Gather and analise your data before you output any of the table, so you will know whether you will need 1 column or 2. How you do that depends on the structure and source of the data in question.

    Basic flow would be something like:

    1) Sql query to get data
    2) split that into a 2d array or 2 arrays
    3) if the 2nd array is not empty, you will need the 2nd <th> tag
    4) output data into table row by row.

    Hope that makes sense.

    getting a little late here so someone else may be able to give you a more efficient solution.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    thank you for your answer.
    It does make sense to have the php code before the definition of the html tags for the tables but what I cannot get is how could I check in the html code what results the php code's queries returned and if there's an empty table or not
  6. #4
  7. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    312
    so you would get all of the data you are going to need and process it, do any calculations you need to, manipulate any strings that need it etc etc. All of that should be done before you output any html.

    Once you have all of your data stored in your variables, and all you need to do is output the html, you would use conditional logic (if statements, case statements etc) to decide if you need to output 1 column or 2 in your table etc. Being that you have all of your data stored in a heap of variables, you should be able to pretty easily see if you have any data for the 2nd column. If you do, output:

    Code:
    <table>
      <tr>
        <th>column 1</th>
        <th>column 2</th>
      </tr>
    then use a loop (for, while etc) to add the data into the table

    PHP Code:
    for($i 0$i count($someArray); $i++) {
      echo 
    "<tr><td>".$someVariable."</td><td>".$someArray['element']."</td></tr>";

    and then carry on as you were.

    Same concept for if you get all of your data together and then discover that you don't need the second row.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?

IMN logo majestic logo threadwatch logo seochat tools logo