Jump to content

Archived

This topic is now archived and is closed to further replies.

glenelkins

can anyone spot the display problem

Recommended Posts

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?

[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>";
  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;
?>
[/code]

Share this post


Link to post
Share on other sites
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:

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

vs.

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

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.

Share this post


Link to post
Share on other sites
You are generating two TD's for the headers while the content generates three TD's
[code]
<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>
[/code]

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
[code]
[0][2] and not [0][1]
[/code]

Also one too many closing TR appear at the end.

This produces correct, but note the renaming two dimentional arrays on top:
[code]
<?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;

?>
[/code]

produces this (a little manually structured source):
[code]
<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>
[/code]

Share this post


Link to post
Share on other sites
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:
[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;
?>[/code]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Thanks alpine..i already changed the code to exactly what you have stated as you were posting lol

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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++)

Share this post


Link to post
Share on other sites
[quote]any ideas?[/quote]

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 [url=http://www.w3schools.com/html/html_tables.asp]tutorial[/url].

Regards
Huggie

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.