This question is an exact duplicate of:
-
I Don't want to show some tables and some column in mysql while generating my record
1 answer
Since I have given to administrator to created the form dynamically.when he created form,What I have done is I have created the table for the form dynamically.Every thing is working fine.Now I want to show specific table and specific column for report generation.
My questions are :
I don't want to show (userlogin,place,venue) table for report generation and three columns (user_id,user_common_id,ayear) these three column are common for all the table which I created dynamically.
Based on every year(2013,2014,2015) I want to generate the report.This i will send in form to mysql while generation report.I want to list what are the forms I have created dynamically.
How to write the query for that.
I have given my code also so far what I have done is here:
<?php
$mysqli = new mysqli("localhost", "root", "", "event");
$result = $mysqli->query("SHOW TABLES");
while ( $row = $result->fetch_row() )
{
$table = $row[0];
echo '<h3>',$table,'</h3>';
$result1 = $mysqli->query("SELECT * FROM $table where ayear='2014'");
if($result1)
{
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
$column = $mysqli->query("SHOW COLUMNS FROM $table");
echo '<tr>';
while($row3 = $column->fetch_row() )
{
echo '<th>'.$row3[0].'</th>';
}
echo '</tr>';
while($row2 = $result1->fetch_row() )
{
echo '<tr>';
foreach($row2 as $key=>$value) {
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
}
$mysqli->close();
?>
Here are the results
(to be filled in)
Here are the expected results
id Date No_of_Days Particulars user_id user_common_id ayear
-- -------- ---------- ---------------- -------- -------------- -----
1 20/07/2013 1 Film Show on... 16 311 1
2 29/08/2013 1 Drama on ... 16 318 1
3 08/03/2014 1 Video Conferen.. 10 621 1
First I present this little chunk, could be useful for others later.
select table_name,column_name,ordinal_position from information_schema.columns
where table_schema = 'event'
order by table_name,ordinal_position
Now on to yours.
$mysqli = new mysqli("localhost", "root", "", "event");
$result = $mysqli->query("SHOW TABLES");
$dontDisplay = array('user_id','user_common_id','ayear');
while ( $row = $result->fetch_row() )
{ $table = $row[0];
echo '<h3>',$table,'</h3>';
$iterationOrdIgnore=array();
$result1 = $mysqli->query("SELECT * FROM $table where ayear='2014'");
if($result1)
{ $ff = $result1->fetch_fields();
$iLoop=0;
foreach ($ff as $oneF) {
if (in_array($oneF->name, $dontDisplay)) {
$iterationOrdIgnore[]=$iLoop;
}
$iLoop++;
}
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
$column = $mysqli->query("SHOW COLUMNS FROM $table where field not in ('user_id','user_common_id','ayear')");
echo '<tr>';
while($row3 = $column->fetch_row() )
{ echo '<th>'.$row3[0].'</th>';
}
echo '</tr>';
while($row2 = $result1->fetch_row() )
{ echo '<tr>';
$iLoop=0;
foreach($row2 as $key=>$value) {
if (!in_array($iLoop, $iterationOrdIgnore)) {
echo "<td>".$value."</td>";
}
$iLoop++;
}
echo '</tr>';
}
echo '</table><br />';
}
}
$mysqli->close();
I used the $dontDisplay
array for the column names not to display.
Each table you work against can be expected to have columns in different ordinal positions (of course). So the array $iterationOrdIgnore
held the ordinal positions for the table you are working on ... to ignore.
fetch_fields() is used against the resultset to get the column names to drive the above $iterationOrdIgnore
.
When the column values are echoed, the ones to ignore, are ignored.
Also, keep a close eye on these two lines far apart from one another.
$dontDisplay = array('user_id','user_common_id','ayear');
and
$column = $mysqli->query("SHOW COLUMNS FROM $table where field not in ('user_id','user_common_id','ayear')");
Combine them somehow for less error-prone cases where you modify one, and not the other. But this was a quick attempt to show it works. It can certainly be cleaned up.
See Manual page for fetch_fields and in_array().
Schema (for others to test)
create table t1
( id int auto_increment primary key,
colQ int not null,
colR int not null,
particulars varchar(50) not null,
user_id int not null,
user_common_id int not null,
ayear int not null
);
insert t1(colq,colr,particulars,user_id,user_common_id,ayear) values (1,1,'blah blah 1',1,1,2014),(2,2,'blah blah 2',2,2,2013),(3,3,'blah blah 3',3,3,2012);
create table t2
( -- different table ordering of columns
id int auto_increment primary key,
particulars varchar(50) not null,
ayear int not null,
colQ int not null,
user_id int not null,
colR int not null,
user_common_id int not null
);
insert t2(colq,colr,particulars,user_id,user_common_id,ayear) values (4,4,'Show 1',1,1,2014),(9,9,'Show 2',2,2,2013),(13,13,'Show 3',3,3,2012);
insert t2(colq,colr,particulars,user_id,user_common_id,ayear) values (99,99,'2014 2014 2014',1,1,2014),(9,9,'Show 2',2,2,2013),(13,13,'Show 3',3,3,2012);