Forum Moderators: coopster
I have rows of values stored in different mysql tables, linked by a primary key 'userid'.
I would like a snippet of code that will print a complete row of values for each person, separated by the table name and commas. This will help me combine the tables into excel later.
user1,TABLE1,1,0,1,1,TABLE2,1,1,1,0,0
user2,TABLE1,0,0,0,1,TABLE2,0,0,1,0,1
Any suggestions?
Thanks
In table1, q1 ... q100
In table2, q101 ... q200
Here is what I've got so far. And I want to:
1. Combine them to it prints one result for each username, linking the two tables together
2. Avoid typing out q1 ... q100 manually
require 'db_connect.php';
require_once('DbConnector.php');
$connector = new DbConnector();
mysql_select_db('table1');
$result = $connector->query("SELECT * FROM table1 WHERE username = 'username' ");
while ($row = $connector->fetchArray($result)){
echo ' '.$row['username'].','.$row['q1'].','.$row['q2'].','.$row['q3'].','.$row['q4'].','.$row['q100'].', ';
}
mysql_select_db('table2');
$result = $connector->query("SELECT * FROM table2 WHERE username = 'username' ");
while ($row = $connector->fetchArray($result)){
echo ' '.$row['username'].','.$row['q101'].','.$row['q102'].','.$row['q103'].','.$row['q104'].','.$row['q200'].', ';
}
"SELECT
table1.*,
table2.*,
FROM table1,table2
LEFT JOIN table1 ON (table1.username = table2.username)
ORDER BY table1.username"
");
while ($row = $connector->fetchArray($result)){
echo ' '.$row['username'].','.$row['q1'].','.$row['q100'].' ';
}
How can I do that without typing them all out individually in the sql statement?
echo 'username,Q1,Q101,Q201<BR>';
$query = "select table1.username AS username, table1.q1 AS q1, table2.q101 AS q101, table3.q201 AS q201
FROM table1
LEFT JOIN table1
ON table1.username=table2.username
LEFT JOIN table3
ON table2.username=table3.username
";
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row separated by commas
while($row = mysql_fetch_array($result)){
echo ' '.$row['username'].','.$row['q1'].','.$row['q101'].','.$row['q201'].',<BR>';
}
$query = "SELECT
table1.*,
table2.*,
table3.*
FROM table1
LEFT JOIN table2 ON table1.username=table2.username
LEFT JOIN table3 ON table1.username=table3.username
";
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row separated by commas
while($row = mysql_fetch_array($result)) {
echo ' '.$row['username'].','.$row['q1'].','.$row['q101'].','.$row['q201'].',<BR>';
}
usernamecolumn in each table though! According to the mysql_fetch_array [php.net] function the last value is going to replace any previous value in the result set! Well, it doesn't matter to us, we are joining
usernameso it will always be the same anyway! So, who cares ;) Now we can picture the data coming back. It probably looks something like ...
Name1, q1, q2, q3, ... Name1, q101, q102, q103, Name1, q201, q202, q203 ... q299
Name2, q1, q2, q3, ... Name2, q101, q102, q103, Name2, q201, q202, q203 ... q299
Name3, q1, q2, q3, ... Name3, q101, q102, q103, Name3, q201, q202, q203 ... q299
Name4, q1, q2, q3, ... Name4, q101, q102, q103, Name4, q201, q202, q203 ... q299
// Print out the contents of each row separated by commas
$out = ''; // initialize
while($row = mysql_fetch_array($result)) {
$out .= $row['name'].','; // start with the name
// column questions run from 1 - 299, and all start with 'q'
for [php.net] ($i = 1; $i <= 299; $i++) {
$out .= $row['q'.$i] . ',';
}
$out = rtrim [php.net]($out, ',') . "\n"; // get rid of the last comma, add a newline
}
print $out;
How can I modify that last bit to get the contents from these tables?
tableh (h1-h40)
table1 (q1-q100
table2 (q101-q200)
table2 (q201-q300)
// Print out the contents of each row separated by commas
$out = ''; // initialize
while($row = mysql_fetch_array($result)) {
$out .= $row['username']. ',';
// start with the name
// column questions run from 1 - 299, and all start with 'q'
for ($i = 1; $i <= 300; $i++) {
$out .= $row['i'.$i] . ',';
}
$out = rtrim($out, ',') . "<BR>"; // get rid of the last comma, add a newline
}
print $out;
I'm almost there! Any help most appreciated :0)
while($row = mysql_fetch_array($result)) {
$out .= $row['username'].','; // start with the name
// column questions run from 1 - 44, and all start with 'h'
for ($i = 1; $i <= 44; $i++){$out .= $row['h'.$i] . ',';}
// column questions run from 1 - 299, and all start with 'q'
for ($i = 1; $i <= 300; $i++){$out .= $row['q'.$i] . ',';}
$out = rtrim($out, ',') . "<BR>"; // get rid of the last comma, add a newline
}
print $out;
Although, to be totally sure it's printing the correct variables, is there a way to have the first line print all the variable names it has called? These could become column headings if I export into excel etc.
Thanks very much for all your help on this, it's looking great.
Yes, there are a number of ways to get the headings out. If you think about what is going on here though you might be able to figure this one out as well. Let me explain. Each $row is an array of data being fetched from the database table. The key word here is array. We know that arrays are made up of key=value pairs, like so:
Array
(
[username] => FirstUser
[h1] => anwser to h1
[h2] => answer to h2
[h3] => answer to h3
.
.
.
[q300] => answer to q300
)
Many thanks for your help with all of this :0)
Any suggestions on how to identify strings of missing values e.g. do not display if there are 3 or more 9s in a row?
for ($i = 0; $i <= 43; $i++){ echo '<th>'.mysql_field_name($result, $i).'</th>'; }
for ($i = 50; $i <= 50; $i++){ echo '<th>'.mysql_field_name($result, $i).'</th>'; }
for ($i = 58; $i <= 58; $i++){ echo '<th>'.mysql_field_name($result, $i).'</th>'; }
for ($i = 1; $i <= 300; $i++){ echo '<th>'.mysql_field_name($result, $i).'</th>'; }
I want field names instead of row names
Yes, you want the column names which are the 'keys' in each $row being read in.
mysql_fetch_array()returns both the associative index as well as a numerical index for each and every column returned in the result set row. The example I showed you only shows the associative index. That is not truly what is being returned by mysql_fetch_array() [php.net] but it is indeed what is being returned by mysql_fetch_assoc() [php.net].
You could check the difference by changing your while loop. Try each of these to see what I mean. Run one to see the array structure, then comment it out and uncomment the other to see the other array structure.
// Try this one first:
while($row = mysql_fetch_array($result)) {
// Try this one next:
// while($row = mysql_fetch_assoc($result)) {
print '<pre>'; print_r($row); exit('</pre>');
// remove the above statement when you are done viewing things.
$out .= $row['username'].','; // start with the name
// column questions run from 1 - 44, and all start with 'h'
for ($i = 1; $i <= 44; $i++){$out .= $row['h'.$i] . ',';}
// column questions run from 1 - 299, and all start with 'q'
for ($i = 1; $i <= 300; $i++){$out .= $row['q'.$i] . ',';}
$out = rtrim($out, ',') . "<BR>"; // get rid of the last comma, add a newline
}
}