issue to pass w3c validator [duplicate] - php

This question already exists:
Closed 10 years ago.
Possible Duplicate:
w3 validation issue
i can't pass the w3c validator for my site ( http://www.bbway.com )
i got 4 error's
1- Line 294, Column 19: end tag for "tr" which is not finished
</td></tr><tr></tr></table>
2- Line 362, Column 19: end tag for "tr" which is not finished
</td></tr><tr></tr></table>
3- Line 400, Column 19: end tag for "tr" which is not finished
</td></tr><tr></tr></table>
4- Line 438, Column 19: end tag for "tr" which is not finished
</td></tr><tr></tr></table>
all the top errors comes from my (multi columns code) i use this code
$getnewbrds=$db->query("select id,userid,title,added_date,active
from brd
where active='1' order by id desc limit 9");
echo '<table width="100%" border="0" cellpadding="5"><tr>';
$count = 0;
$max = 3;
while($fetch_newbrds=$db->fetch($getnewbrds)){
$count++;
$getbrduser = get_val("id,name","users","where id='$fetch_newbrds[userid]'","name");
$getbrduseri = get_val("id,name","users","where id='$fetch_newbrds[userid]'","id");
echo '<td align="center" width="30%" valign="top">
<table border="0" width="100%" cellspacing="3" cellpadding="3">
<tr>
<td height="50" colspan="2" align="center">
<a href="showbrd-'.$fetch_newbrds['id'].'.html" title="'.$fetch_newbrds['title'].'">
'.$fetch_newbrds['title'].'</a>
</td>
</tr>
<tr>
<td colspan="2" align="right" class="brduseronmain">
<a href="users-'.$getbrduseri.'.html" title="'.$getbrduser.'">'.$getbrduser.'
<span>'.$fetch_newbrds['added_date'].'</span>
</a>
</td>
</tr>
</table>
</td>';
if($count >= $max){
$count = 0;
echo '</tr><tr>';
}
}
echo '</tr></table>';
could you please share your fixes for this issue.

Your issue is here:
if($count >= $max){
$count = 0;
echo '</tr><tr>';
}
You need to add logic so that on the last iteration it doesn't output an empty row.
You could do this with another counter or better yet don't pre-wrap the <tr> outside the while.

Here is your code, fixed, I think, didn't test it since you didn't provide data I could test with it, find more info below.
$getnewbrds=$db->query("
select
id,
userid,
title,
added_date,
active
from
brd
where
active='1'
order by
id desc
limit 9
");
$count = 0;
$max = 3;
$html = '<table width="100%" border="0" cellpadding="5">';
while($fetch_newbrds = $db->fetch($getnewbrds)) {
$count++;
if($count % $max === 1){
$html .= '<tr>';
}
$getbrduser = get_val("id,name","users","where id='$fetch_newbrds[userid]'","name");
$getbrduseri = get_val("id,name","users","where id='$fetch_newbrds[userid]'","id");
$html .= '
<td align="center" width="30%" valign="top">
<table border="0" width="100%" cellspacing="3" cellpadding="3">
<tr>
<td height="50" colspan="2" align="center">
<a href="showbrd-'.$fetch_newbrds['id'].'.html" title="'.$fetch_newbrds['title'].'">
'.$fetch_newbrds['title'].'
</a>
</td>
</tr>
<tr>
<td colspan="2" align="right" class="brduseronmain">
<a href="users-'.$getbrduseri.'.html" title="'.$getbrduser.'">'.$getbrduser.'
<span>'.$fetch_newbrds['added_date'].'</span>
</a>
</td>
</tr>
</table>
</td>
';
if($count % $max === 0){
$html .= '</tr>';
}
}
if($count % $max !== 0) {
while($count % $max !== 0) {
$html .= '<td></td>';
$count++;
}
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
Few things I did
Indented your code properly; do that yourself before even trying to
post a question
Captured the html in a variable first echo'ed when
done; right to the output is not the best way...
I used modulus to figure out if $count divided by $max leaves 1 to determine if I should open a new <tr>... and did the same check to see if the remainder would be 0 to see if I need to close it... Then at last after the while I check if the
was closed or not, if not add <td></td> until it is to keep the html valid and eventually close the last <tr>.

Related

how to sum array values from "for" loop

Here is my code :
<tr align="right">
<td style="white-space: nowrap" align="left"><?= $dt_left_header ?></td>
<td></td>
<?php
//trx data
for($p=0; $p < count($arr_prd_grp); $p++){
$prd_id = $arr_prd_grp[$p] ;
//print_r($arr_prd_grp[$p]);
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
}
}
//TOTAL
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($amt_tot += $arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
}
?>
</tr>
In this case, I want to calculate total of $arr_amt[$coa_id][$prd_id] . My code already calculate it but the result is not equal with my expectation. Can someone tell me how to make it right? Thanks
Move the sum calculation to the first loop, then show the result in the appropriate place. To made it easier i've made an extra variable $totalAmount;
Also my guess is that you are having another outer loop (maybe for each table row). Your current code did not default the totalAmount to 0, so it was adding all the ammounts of each rows, thats why you resulted in such a big number. We add a default value 0 for each row to help that.
<tr align="right">
<td style="white-space: nowrap" align="left"><?= $dt_left_header ?></td>
<td></td>
<?php
//trx data
$totalAmount = 0; // default it
for($p=0; $p < count($arr_prd_grp); $p++){
$prd_id = $arr_prd_grp[$p] ;
//print_r($arr_prd_grp[$p]);
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
$totalAmount+=$arr_amt[$coa_id][$prd_id];
}
}
//TOTAL
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($totalAmount, 2,',','.').'</td>
';
}
?>
</tr>

Need to pull data from MySQL data base using PHP to read dynamic checkboxes

I am trying to pull data from mysql table using dynamic checkboxes created previously, however it looks like my search only displays 2 records max and if I select more than 3 checkboxes it will not return anything, so I was wondering if someone could help me figure out how to do it.
Below is my code, I really appreciate your help:
$warehouse = #$_POST['wh'];
switch($button){
case 'Submit':
if(#$_POST['wh']){
$warehouse = #$_POST['wh'];
$length = count($warehouse);
for ($i = 0; $i < $length; $i++){
//echo '<br>'.$warehouse[$i];
}
$consult = "SELECT * FROM contact_info WHERE ";
for ($i = 0; $i < $length; $i++){
$consult = $consult . "warehouse='$warehouse[$i]'";
if(!$i+1 == $length){
$consult = $consult . " OR ";
}
}
echo '<br>'.$length.'<br>';
print_r ($consult);
$response = mysqli_query($connection, $consult);
if($response){
$registry = mysqli_affected_rows($connection);
if($registry > 1){
echo '<br><table width="800" align="center" border="2" cellspacing="1" cellpadding="1">
<form action="index.php" method="post" >
<tr>
<td align="center"><strong>Warehouse</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Phone</strong></td>
<td align="center"><strong>I-net</strong></td>
<td align="center"><strong>E-mail</strong></td>
</tr>';
while($registry = mysqli_fetch_array($response, MYSQLI_ASSOC)){
echo '<form action="index.php" method=post >
<tr>
<td div align="center">'.$registry['warehouse'].'
<td div align="center">'.$registry['name'].'
<td div align="center">'.$registry['lastname'].'
<td div align="center">'.$registry['phone'].'
<td div align="center">'.$registry['inet'].'
<td div align="center">'.$registry['email'].'
</tr>';
}
echo '</form>';
echo '
</form>
</table>';

Removing the last element in a do-while

How can I remove the last element in a do-while generated table?
In my case the last tr/td where div.dividing_line is stored.
The code:
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
++$i;
} while ($i < $ArrayLength+1);
For example: If I have an array with 6 items, normally the do-while will do the job, so finally there will be 6 tr's with data and 6 tr's with the dividing_line.
What I need is 6 tr's of data and 5 tr's of dividing_line. Is that possible?
Try this-
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i != $ArrayLength) {
echo '<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
}
++$i;
} while ($i < $ArrayLength+1);
Use an extra if Statement to check whether you are at the last element:
if (%i < $ArrayLength) { echo '<tr>...dividing_line</tr>'; }
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i < $ArrayLength)
{
echo '
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>';
}
++$i;
} while ($i < $ArrayLength+1);
I think your approach just needs an additional step.
It could be something like this:
$data = null;
foreach ($rows as $row) {
$data[] = "<tr><td valign=\"middle\">Data_Position</td><td valign=\"middle\">Data_Item</td><td valign=\"middle\">Data_Pieces</td><td valign=\"middle\">Data_Price</td></tr>";
}
print implode("<tr><td colspan=\"4\"><div class=\"dividing_line\"></div></td></tr>", $data);
This way, you could accomplish what you want without any more logic. Of course, it can be changed or re-design, but I think this way will provide you with a simple yet elegan solution to your problem.
Hope it helps :P

php explode product array throwing up errors

I have set up an 'orders' page that should be pretty straight-forward, requiring a for each loop to generate past orders from the DB. Within each loop, there needs to be an explode to separate each item(,), then another explode to pull the details out of each item(-). I was wondering if anyone could tell me why I'm getting the error message "Warning: mysql_num_rows() expects parameter 1" at the end of each iteration of my foreach loop? It's particularly confusing for me because the code still works, which I don't think it should if the error was true.
<?php
$sql = "SELECT * FROM orders WHERE store='$user'";
$res = mysql_query($sql);
$arrOrders = array();
while ($row = mysql_fetch_array($res)) {
array_push($arrOrders, $row);
}
?>
<table width="85%" align="center" border="0" cellpadding="5">
<?php if (count($arrOrders) > 0) { ?>
<?php foreach ($arrOrders as $key => $value) { ?>
<tr>
<td valign="top" style="font-weight: bold">
ID #<?=$value['id']?>
</td>
<td valign="top" style="font-weight: bold">
Status: <?=$value['status']?>
</td>
<td valign="top" style="font-weight: bold">
Order #<?=$value['order_ref']?>
</td>
<td align="left" valign="top" style="font-weight: bold">
<?php
$tmpProds = explode(',', $value['products']);
foreach ($tmpProds as $key2 => $value2) {
$tmpProdInfo = explode('-', $value2);
$sql2 = 'SELECT * FROM products_full WHERE id = ' . $tmpProdInfo[0];
$res2 = mysql_query($sql2);
if (mysql_num_rows($res2) > 0) { // THIS IS THE ROW THAT THE ERROR MESSAGE POINTS TO
echo $tmpProdInfo[1] + $tmpProdInfo[2] . ' x ' . mysql_result($res2, 0, 'alpha_code') . ' (' . trim(mysql_result($res2, 0, 'description')) . ')<br /><br />';
}
}
?>
</td>
</tr>
<tr>
<td width="100%" colspan="5" align="center">
<hr style="width: 100%" />
</td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td width="100%" align="center">
There are currently no complete orders.
</td>
</tr>
<?php } ?>
</table>
Thanks in advance, I think I've included everything that is relative but if anything else is needed please let me know.
Joe
Try this:
$sql2 = 'SELECT * FROM products_full WHERE id = '.$tmpProdInfo[1].'';
if (count($res2) > 0)
Also, be aware when you want to use MySQL_num_rows you should include the connection. see the manual:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

PHP MYSQL, How to show records in four columns ,

Here is my code the records shows in four columns but if my records is blank it shows three balng images, any suggestions?
$query = mysql_query("SELECT * from rbf_events_images where event_id='".$_GET['id']."'");
echo '<table border="1">';
if(count(mysql_num_rows($query)>0)):
$tropentags='<tr>';
$troclosingtags='</tr>';
$formTags="";
$tdTags="";
$count=1;
while($row = mysql_fetch_array($query)){
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
if ($count>3)
{
$formTags.=$tropentags.$tdTags.$troclosingtags;
$tdTags="";
$count=0;
}
$count=$count+1;
}
if ($count>0)
{
for($i = 1; $i <= (4-$count) ; $i++)
{
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
}
$formTags.=$tropentags.$tdTags.$troclosingtags;
}
echo $formTags;
endif;
Thanks for your help!really appreciated!
I noticed that on lines like this one:
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
You are delimiting the string with single quotes ('), and you are also trying to embed a variable in the string that uses single quotes. I'm not sure how you did not get compile errors for that. I would switch to:
$tdTags= '<td align="left" valign="middle" class="td">' . $row['image'] . '</td>';
Here's what I usually do to put records in columns:
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * from rbf_events_images where event_id='$id'");
echo '<table border="1"><tbody><tr>';
if (mysql_num_rows($query) > 0) {
$count = 0;
while ($row = mysql_fetch_array($query)) {
if ($count && $count % 4 == 0) echo '</tr><tr>';
echo '<td align="left" valign="middle" class="td">'.$row['image'].'</td>';
$count++;
}
}
echo '</tr></tbody></table>';

Categories