Jump to content


Photo

can anyone spot the display problem


  • Please log in to reply
9 replies to this topic

#1 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 02:01 PM

If you paste this code into notpad and save as a php file, then goto the file in IE... you will see its generating the table from the array information. BUT!!! If you notice that the "Number Of Sites" column, the data is not appearing in line line the "Company Name" data...can anyone spot why this is happening?

<?

$pops_info[0][0] = "Name Test";
$pops_info[0][2] = 2;	

$pops_info[1][0] = "Name Test2";
$pops_info[1][2] = 3;



// Display
$arr = array (
	'fields' => 2,
	'direction' => 'vert',
	'field_headings' => array ("Company Name","Number Of Sites"),
	'field_contents' => array($pops_info)
);

$table = "<table border='0' align='center' width='80%'>
	  <tr>";

foreach ($arr['field_headings'] as $fh) {
  $table .= "
    <td align='center'>
      <b>" . $fh . "</b>
    </td>";
}

$table .= "</tr>";

foreach ($arr['field_contents'][0] as $fc){
  $table .= "<tr>";
  for ($i=0;$i<count($arr['field_contents'][0])+1;$i++) {
    $table .= "
      <td align='center'>
        " . $fc[$i] . "
      </td>";
  }
  $table .= "</tr>";
}

$table .= "</tr></table>";

echo $table;
?>

http://thewebsolutionprovider.com - A Solution For Everything Web!

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 26 October 2006 - 02:23 PM

first of all, i don't see why you're using a for() loop when you know you'll be wanting to go through the whole contents array:

for ($i=0;$i<count($arr['field_contents'][0])+1;$i++) {
    $table .= "
      <td align='center'>
        " . $fc[$i] . "
      </td>";
  }

vs.

foreach ($arr['field_contents'][0] AS $v) {
    $table .= "
      <td align='center'>
        " . $v . "
      </td>";
  }

your array key numbers are effed up in the for().  your upper limit is silly (why would you go to the array count + 1 when you're starting at 0?), so you'll run through it too many times and consequently echo too many cells.  eradicate these math issues using the foreach().

that being said, your second value should be in $pops_info[0][1], not $pops_info[0][2].

if you're using a for() simply because you want to stick to the key order, use a sorting function on the array before you run it.

#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 26 October 2006 - 02:26 PM

You are generating two TD's for the headers while the content generates three TD's
<table border='0' align='center' width='80%'>
<tr>
<td align='center'><b>Company Name</b></td>
<td align='center'><b>Number Of Sites</b></td>

</tr><tr>

<td align='center'>Name Test</td>
<td align='center'></td>
<td align='center'>2</td>

</tr><tr>

<td align='center'>Name Test2</td>
<td align='center'></td>
<td align='center'>3</td>

</tr></tr>
</table>

Without knowing the purpose or the rest of your code and it's structure, by changing the two dimentional arrays with content along with a small adjustment in your for(), it is solved. But i guess its for a reason you named each array
[0][2] and not [0][1]

Also one too many closing TR appear at the end.

This produces correct, but note the renaming two dimentional arrays on top:
<?php

$pops_info[0][0] = "Name Test";
$pops_info[0][1] = 2;

$pops_info[1][0] = "Name Test2";
$pops_info[1][1] = 3;

// Display
$arr = array (
	'fields' => 2,
	'direction' => 'vert',
	'field_headings' => array ("Company Name","Number Of Sites"),
	'field_contents' => array($pops_info)
);

$table = "<table border='0' align='center' width='80%'><tr>";

foreach ($arr['field_headings'] as $fh) {
$table .= "
<!-- each heading start -->
    <td align='center'>
      <b>" . $fh . "</b>
    </td>
<!-- each heading stop -->";
}

$table .= "</tr>";

foreach ($arr['field_contents'][0] as $fc){
  $table .= "<tr>";
  for ($i=0;$i<count($arr['field_contents'][0]);$i++) {
    $table .= "
    <!-- each content start -->
      <td align='center'>
        " . $fc[$i] . "
      </td>
    <!-- each content end -->";
  }
  $table .= "</tr>";
}

//$table .= "</tr></table>";
$table .= "</table>";

echo $table;

?>

produces this (a little manually structured source):
<table border='0' align='center' width='80%'>
<tr>

<!-- each heading start -->
<td align='center'><b>Company Name</b></td>
<!-- each heading stop -->

<!-- each heading start -->
<td align='center'><b>Number Of Sites</b></td>
<!-- each heading stop -->

</tr><tr>

<!-- each content start -->
<td align='center'>Name Test</td>
<!-- each content end -->

<!-- each content start -->
<td align='center'>2</td>
<!-- each content end -->

</tr><tr>

<!-- each content start -->
<td align='center'>Name Test2</td>
<!-- each content end -->

<!-- each content start -->
<td align='center'>3</td>
<!-- each content end -->

</tr></table>


#4 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 26 October 2006 - 02:33 PM

Yes, you are getting a blank td cell between the two columns. Why? Because of your for statement and beacus of the way you created your array.

In your array you have this (notice that the first item has an indext of 0 and the 2nd item has an index of 2):

$pops_info[0][0] = "Name Test";
$pops_info[0][2] = 2;

Then in your foreach statement you use this:

for ($i=0;$i<count($arr['field_contents'][0])+1;$i++) {

Since you are starting at 0 and continuing until [the count of the array (2) plus 1, i.e. 3] you are going through the loop three times - plus you are attempting to grap the value for the 1 index which doesnt exist. you should have just used another foreach.

Also, you put a closing TD at the very end which was not correct since you were already closing your TRs in the loop. And, I think you are making the use of arrays in this example more complex than it needs to be, IMHO.

Corrected code:
<?

$pops_info[0][0] = "Name Test";
$pops_info[0][2] = 2;	

$pops_info[1][0] = "Name Test2";
$pops_info[1][2] = 3;



// Display
$arr = array (
	'fields' => 2,
	'direction' => 'vert',
	'field_headings' => array ("Company Name","Number Of Sites"),
	'field_contents' => array($pops_info)
);

$table = "<table border='0' align='center' width='80%'>
	  <tr>";

foreach ($arr['field_headings'] as $fh) {
  $table .= "
    <td align='center'>
      <b>" . $fh . "</b>
    </td>";
}

$table .= "</tr>";

foreach ($arr['field_contents'][0] as $fc){
  $table .= "<tr>";
  foreach ($fc as $value) {
    $table .= "
      <td align='center'>
        " . $value . "
      </td>";
  }
  $table .= "</tr>";
}

$table .= "</table>";

echo $table;
?>

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 02:34 PM

Hi

I need the For loop in there! trust!

But your second comment about taking out the +1 and changing the $pops_info[0][1] is correct that solved the problem!
http://thewebsolutionprovider.com - A Solution For Everything Web!

#6 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 02:35 PM

Thanks alpine..i already changed the code to exactly what you have stated as you were posting lol
http://thewebsolutionprovider.com - A Solution For Everything Web!

#7 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 03:22 PM

Hi

Something else has come up with this! It works fine for items for example:

$pops_info[0][0] = "test";
$pops_infp[0][1] = "jnfjdfn";

$pops_info[1][0] = "test2";
$pops_info[1][1] = "nkgnfg";

but if i add extra fields as such, it does not display the extra ones??

$pops_info[1][0] = "test2";
$pops_info[1][1] = "nkgnfg";
$pops_info[1][2] = "jngjkfgnfg"; <-- wont display this one

any ideas?
http://thewebsolutionprovider.com - A Solution For Everything Web!

#8 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 03:25 PM

though it does seem to work if i add say +1 in the for loop

for ($i=0;$i<count($arr['field_contents'][0])+1;$i++)

or say +2 for 2 extra fields

for ($i=0;$i<count($arr['field_contents'][0])+2;$i++)
http://thewebsolutionprovider.com - A Solution For Everything Web!

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 03:28 PM

any ideas?


I'd imagine that you don't have a very good understanding of HTML tables if you're not sure why this won't display properly.  They're very rigid structures, so attempting to add another column just 'willy nilly' in the middle of a table isn't just going to work.

Take a look at this tutorial.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 27 October 2006 - 02:37 AM

furthermore, i'm not sure you've got an excellent grasp on for() and foreach() loops.  my foreach() loop will do exactly what you want it to everytime, if you'll only stop and consider using it.  i know you've said you NEED the for() loop there; if that's the case, why so?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users