Forum Moderators: coopster
Been thinking about a solution for this for a few hours now and run out of ideas!
Background:
there are 2 tables, 1 table holds information on what data we want to display from table 2 (which holds the data)
Table1 SQL =>
$sql = "SELECT * FROM ".$site_prefix."_sm_".$tableprefix."_editconfig WHERE ModSetID='$moduleid' AND ( _view = '$UserGroup' OR _view < '$UserGroup' ) AND Overview='1' ORDER BY FieldOrder ASC";
$result = mysql_query($sql) or die("<div class='notice'>Error: " . mysql_error() ."</div>");
for( $i = 0; $i < $tableheading = mysql_fetch_array($result); $i++){
// Get Table Headings
echo "<th class='sorttable_nosort' title='".$tableheading['Description']."'>".$tableheading['Label']."</th>"
$table=$tableheading["Table"];
$fields=$fields.$site_prefix."_".$table.".".$tableheading["Column"].",";
$inputarray[]=$tableheading["InputType"];
}
Included in Table1 is the column $tableheading['InputType'] we will need this for later.
Table2 SQL =>
$fields=substr($fields,0,strlen($fields)-1);
$sql2 = "SELECT ".$site_prefix."_".$table.".id,".$fields." FROM ".$site_prefix."_".$table." ".$join." ORDER BY ".$site_prefix."_".$table.".".$orderby." ASC LIMIT ".$limitstart.",".$limit."";
$result2 = mysql_query($sql2) or die("<div class='notice'>Error: " . mysql_error() ."</div>");
for( $i = 0; $i < $tabledata = mysql_fetch_row($result2); $i++){
?>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $tabledata[0];?>" id="cb<?php echo $i;?>" onclick="isChecked(this.checked);" /></td>
<?php
unset($tabledata[0]);
foreach($tabledata as $row){
if(($inputarray[$i] == 'checkbox') && ($row == '1')){
echo "<td title='".$inputarray[$i]."'>True</td>";}elseif(($inputarray[$i] == 'checkbox') && ($row == '0')){
echo "<td title='".$inputarray[$i]."'>False</td>";
}else{
echo "<td title='".$inputarray[$i]."'>$row</td>";
}
$i++;
}
}
Issue
The table returned should (ongoing on current data) display 4 rows 4 columns.
At the moment only the first row returns the inputarray[$i] value
Not sure if that makes enough sense.
Thanks
foreach($tabledata as $row){
if(($inputarray[$i] == 'checkbox') && ($row == '1')){
echo "<td title='".$inputarray[$i]."'>True</td>";}elseif(($inputarray[$i] == 'checkbox') && ($row == '0')){
echo "<td title='".$inputarray[$i]."'>False</td>";
}else{
echo "<td title='".$inputarray[$i]."'>$row</td>";
}
$i++;
}
echo "<td title=\"".$inputarray[$i]."\">True</td>";}elseif(($inputarray[$i] == 'checkbox') && ($row == '0')){
echo "<td title=\"".$inputarray[$i]."\">False</td>";
}else{
echo "<td title=\"".$inputarray[$i]."\">$row</td>";
You have
for( $i = 0; $i < $tabledata = mysql_fetch_row($result2); $i++){then further down you are looping through the same $i, almost at the end of the script. Do you need another variable to track the looping?
Thanks for your reply. I have carried out your recommendation but I am still unable to achieve the results i want. Perhaps it is in the logic of doing it rather than the code itself?
Maybe if i explain in terms of data and tables when you can see what result i am trying to get.
table1
id ¦ Table ¦ Column ¦ Label ¦ InputType ¦ RefTable ¦RefColumn
This table holds information on what we want the user to be able to view and edit in the cms.
So table relates to the main table (e.g. pages), Column is the column name in Table, Label is the name we are giving to the column, inputype would be like checkbox/wysiwyg/text (each row will have this filled in), RefTable relates to the name of any joining table needed, RefColumn is the Name we want to display from the Joining Table.
table2 (example data table - pages)
id ¦ ParentID ¦ PageName ¦ PageHeadline ¦ PageSection ¦ PageActive
ParentID links within the table to the relevant id (so if it is a main section then ParentID would be 0)
Table1 is for the cms and table2 is for the front-end
The below is where i have got the data from Table1 and display the Table Headings
$sql = "SELECT * FROM ".$site_prefix."_sm_".$tableprefix."_editconfig WHERE ModSetID='$moduleid' AND ( _view = '$UserGroup' OR _view < '$UserGroup' ) AND Overview='1' ORDER BY FieldOrder ASC";
$result = mysql_query($sql) or die("<div class='notice'>Error: " . mysql_error() ."</div>");for( $i = 0; $i < $tableheading = mysql_fetch_array($result); $i++){
// Get Table Headings
echo "<th class='sorttable_nosort' title='".$tableheading['Description']."'>".$tableheading['Label']."</th>"
$table=$tableheading["Table"];
$fields=$fields.$site_prefix."_".$table.".".$tableheading["Column"].",";
$inputarray[]=$tableheading["InputType"];
}
I then did the below to get the details of the pages from Table2
$fields=substr($fields,0,strlen($fields)-1);$sql2 = "SELECT ".$site_prefix."_".$table.".id,".$fields." FROM ".$site_prefix."_".$table." ".$join." ORDER BY ".$site_prefix."_".$table.".".$orderby." ASC LIMIT ".$limitstart.",".$limit."";
$result2 = mysql_query($sql2) or die("<div class='notice'>Error: " . mysql_error() ."</div>");
for( $j = 0; $j < $tabledata = mysql_fetch_row($result2); $j++){
What i then want to display is the data from the Table2 but also each column will then have its value InputType from Table1. I just placed it in the title attribute for testing purposes.
Does that make anymore sense?
for( $i = 0; $i < $tableheading = mysql_fetch_array($result); $i++){
$tableheading = mysql_fetch_array($result);
// kept $tableheading as I didnt see if you were using this further
// down your code.
$tableheadingLen = count($tableheading);
for ($i=0; $i<$tableheadingLen; $i++) {
The mysql_fetch_row also returns an array, so your other loop may well work better if you use count(var) to get an interger value.
Hope that makes some difference :)
Results below are what I am trying to get
array from table1
=> text
=> text
=> selectparent
=> checkbox
results from table2 (dummy results)
column names = Page Title,Headline,ParentID,Active
row1 (welcome,welcome to my site, NULL, 1)
row2 (about,about my site,home,1)
row3 (contact,contact me,NULL,0)
what i want to display in my table is:
<tr>
<td title="text">welcome</td>
<td title="text">welcome to my site</td>
<td title="selectparent"></td>
<td title="checkbox">1</td>
</tr>
<tr>
<td title="text">about</td>
<td title="text">about my site</td>
<td title="selectparent">home</td>
<td title="checkbox">1</td>
</tr>
<tr>
<td title="text">contact</td>
<td title="text">contact me</td>
<td title="selectparent"></td>
<td title="checkbox">0</td>
</tr>
the results i currently get are either where the text,selectparent etc are only displayed on the first row of the table OR I get all of the text first then all of the selectparents then all of the checkbox's so end up with something like:
<tr>
<td title="text">welcome</td>
<td title="text">welcome to my site</td>
<td title="text"></td>
<td title="text">1</td>
</tr>
<tr>
<td title="text">about</td>
<td title="text">about my site</td>
<td title="selectparent">home</td>
<td title="selectparent">1</td>
</tr>
<tr>
<td title="selectparent">contact</td>
<td title="checkbox">contact me</td>
<td title="checkbox"></td>
<td title="checkbox">0</td>
</tr>
um... i am sure that producing the correct result is possible as i know it has been done (but not seen the code) but am not sure if the way i am trying to do it is correct.
Basically there can be any number of titles returned from table1
Maybe another way to put the question is like this.
i have now used
$numCols = count($inputarray);
to count the number of columns from Table1 so for example i know that this value is 4.
I then know that my table of results will have 4 columns in it.
I then use the below to get my data for the results table PER COL.
How can i loop through my array so that if my array is array(text,text,selectparent,checkbox) then for each column below that is returned it returns the $input in the order of the array and keeps on going until all of the columns from table2 are displayed.
foreach($tabledata as $col){
if(($input == "checkbox") && ($col == "1")){
echo "<td title=\"".$input."\">True</td>";
}elseif(($input == "checkbox") && ($col == "0")){
echo "<td title=\"".$input."\">False</td>";
}else{
echo "<td title=\"".$input."\">$col</td>";
}
}