4

the SQL statement

$CustInfo = Select * from wp_wpsc_submited_form_data where log_id = '6';

results in the following:

ID    CUST_ID    FORM_ID    VALUE

81       6          2       John
82       6          3       Smith
83       6          4       123 Main Street
84       6          5       Houston
96       6          6       NULL
85       6          7       US
86       6          8       77459

I have been scratching my head for a few hours with all the example here on Stack Overflow, but can't see to find anything close enough that is understandable. I am hoping someone can plainly and simply explain and may be provide an example as to how I can generate an HTML table with the values.

example:

Firstname :   <%php echo $CustInfo[2]; ?>
Lastname :    <%php echo $CustInfo[3]; ?>
Address :    <%php echo $CustInfo[4]; ?>

etc...

jonsca
  • 10,218
  • 26
  • 54
  • 62

3 Answers3

2

How about something like this: (untested)

if(is_array($CustInfo) && count($CustInfo) > 0)
{
  echo "<table>";
  for($i=0;$i<count($CustInfo);$i++)
  {
     echo "<tr>";
     switch($i)
     {
       case 1: echo "<td>FirstName:</td><td>".$CustInfo[i]."</td>";
       ...
     }
     echo "</tr>";
  }
  echo "</table>";
}

I check to see if the query returned a result with is_array and count() then iterator through the array using a switch/case to determine what the label to use. Just add case 2 to 8 and this should do the trick.

EDIT: An alternative would be to use a variable to the store the HTML and echo it after the process has completed.

Todd Moses
  • 10,969
  • 10
  • 47
  • 65
  • tried this... but it generates a blank screen... but if i do an echo of my select it shows there is data – user1684647 Sep 21 '12 at 13:44
  • @user1684647 This example requires $CustInfo to be a returned array from the result of your SQL query, not the text of the query. – Todd Moses Sep 21 '12 at 15:53
  • Sorry for my ignorance.. I'm learning this as i go by example and dont mean to be a burden.. can you explain what you mean by "example requires $CustInfo to be a returned array from the result of your SQL query, not the text of the query" I think what you're trying to say is that $CustInfo is the output of the execution of the query... and not the "Select * From .... where ... X=''"... please confirm – user1684647 Sep 21 '12 at 17:20
  • @user1684647 That is correct, it is the output and not the "Select...". – Todd Moses Sep 21 '12 at 17:50
0

This nasty bit of code should do the trick, it uses a group_concat to group non null rows into a single field nd as the unions mean only one column is null for each row, it should work:

select 
    group_concat(Firstname) as FirstName,
    group_concat(Surname) as Surname,
    group_concat(Add1) as Add1,
    group_concat(Add2) as Add2,
    group_concat(Add3) as Add3,
    group_concat(Country) as Country,
    group_concat(ZipCode) as ZipCode
from
(
    select
        value as FirstName,
        null as Surname,
        null as Add1,
        null as Add2,
        null as Add3,
        null as Country,
        null as ZipCode
    from 
        wp_wpsc_submited_form_data 
    where 
        log_id = '6' // I can't actually see this field in your columns??
        and form_id=2
    union
    select
        null as FirstName,
        Value as Surname,
        null as Add1,
        null as Add2,
        null as Add3,
        null as Country,
        null as ZipCode
    from 
        wp_wpsc_submited_form_data 
    where 
        log_id = '6'
        and form_id=3
    union
    ...
    ...
    ...
    union
    select
        null as FirstName,
        null as Surname,
        null as Add1,
        null as Add2,
        null as Add3,
        null as Country,
        Value as ZipCode
    from 
        wp_wpsc_submited_form_data 
    where 
        log_id = '6'
        and form_id=8
)

Then you should be able to iterate nicely through the rows and do this:

Firstname :   <%php echo $CustInfo['FirstName']; ?>

And so on.

I also wrote a length Q&A you might be interested in here: How can an SQL query return data from multiple tables

Edit: I see that the other two folks who answered told you to redesign your table - which I sort of do, but sort of don't agree with.

This table is ruggedly normalized, which means you can add a hundred extra form fields and not have to touch the table itself - that's a (amazingly) good thing. I would say that this design is making it harder for you to get this particuar data out, but it very robust - if not amazingly quick or easy to access.

Community
  • 1
  • 1
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • Tried this... on the web page it was blank.. no errors.. so i decided to run this query directly on mysql and the result was [Err] 1248 - Every derived table must have its own alias... not sure what this means... i've tried to look it up and as the error suggests, there is no alias for the derived tables... the only other draw back with this method (an i'm no expert) is that if i had additional dynamic fields then i'd have to remember to touch this file again.. compare to the array method and looping through the array results... – user1684647 Sep 21 '12 at 14:02
0

The issue is the database design, not the PHP or SQL.

Your table should look like this:

| CUST_ID (PK) |  FIRSTNAME | LASTNAME | STREETADDRESS   | CITY    | STATE | ZIPCODE |
 ------------------------------------------------------------------------------------
| 6            | John       | Smith    | 123 Main Street | Houston | NULL  | 77459   |
| 7            | Jane       | Doe      | 456 Fake St.    | Richmond| VA    | 23860   |

From here, you can call these by simply using the SQL Statement:

SELECT *
FROM UserTable
WHERE CUST_ID = 6

This will return you a simple answer, which you can use like so:

FirstName: <%php echo $CustInfo['FIRSTNAME']; ?>

(Disclaimer: I have limited PHP knowledge, but I think this will work. If not, I do know it's not much more complicated)

Much much simpler, much less code... and most importantly imho, MUCH more efficient.

I do suggest you read up on Database Normalization for better understanding on how Databases should be designed. But it's not needed for this, just something good to know and understand.

mawburn
  • 2,232
  • 4
  • 29
  • 48
  • unfortunately I'm modifying a pre-existing script that was purchased, which i need to integrate with another site to transfer data... I was hoping it would be as simple as this... The end result of your code is truly what I'm looking for.. being able to use a variable and place it anywhere i need in the script... but thanks for suggesting – user1684647 Sep 21 '12 at 13:48
  • That sucks. It would probably be easiest to parse the information directly with code, rather than SQL. Personally, I would just do a bunch of small select statements to get the information I needed for each field. Horrible solution, but it would be quick and easy to implement. Sorry I couldn't help. – mawburn Sep 21 '12 at 21:25