Forum Moderators: coopster

Message Too Old, No Replies

Problem Solving Arrays and Foreach Loops

         

dwighty

10:00 pm on Oct 18, 2007 (gmt 0)

10+ Year Member



Hi Guys,

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

PHP_Chimp

7:32 pm on Oct 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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++;
}

You are using ' around the title attributes. Those should be ", so you will need to escape them to get your attributes properly quoted in the output.
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>";

There are a few other places where you have html tags using ' as opposed to " to quote your attributes...in fact the more I read it looks as though almost all of them are using '.

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?

dwighty

12:12 pm on Oct 20, 2007 (gmt 0)

10+ Year Member



Hi PHP_Chimp,

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?

PHP_Chimp

7:29 pm on Oct 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Had another look -
for( $i = 0; $i < $tableheading = mysql_fetch_array($result); $i++){

The mysql_fetch_array returns an array. So your loop would evaluate to something like -
start with $i=0, stop when $i less than ARRAY, $i++.
This is the same for both sets of looping.
How about -


$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 second set of for loops is not using an integer as the middle argument. To be honest I'm not sure if what you are doing works properly, as I would always try to use integers as the first and second arguments in a for loop.

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 :)

dwighty

2:35 pm on Oct 23, 2007 (gmt 0)

10+ Year Member



I have had more time to play around and try the options you suggested but i seem to get 2 different results both of which are not quite there.

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.

PHP_Chimp

4:46 pm on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seeing as you dont need the titles to change then cant you hard code those in?
i.e.
$op = "<td title=\"text\">$inputarray[0]</td>\n";
$op.= "<td title=\"text\">$inputarray[1]</td>\n";
// the other 2 parts of the array
echo $op;

What code are you using to get the output you gave in your last post?

dwighty

8:20 am on Oct 24, 2007 (gmt 0)

10+ Year Member



The titles are not the same in every instance. The data i provided was just an exmaple.

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>";
}

}

dwighty

1:36 pm on Oct 24, 2007 (gmt 0)

10+ Year Member



Just to say that this I have been able to fix this now.

Thanks for your help php_chimp.