display only specific value of array - php

I need to display specific value of key 'pasdiz_alus' from array $form->data into a table cell. And I need to display this table row only if value of key 'pasdiz_alus' is greater than '0'.
The code for this is below, but the problem is that output displays also the value of key 'pasdiz_alus' above my table row and there it is displayed as many times as number of keys of array.
How can I get rid of this display of value of " 'pasdiz_alus' x times of number of keys in array (in my case 29 times - there are 29 keys in the array)"? In this case it is: 5454545454545454......
My code is:
<table style="width: 800px;">
<tbody>
<?php
if ($form->data['pasdiz_alus'] > 0){
echo '<tr><td style="width: 100px;">Bilde šeit</td><td style="width: 500px;"> <strong>Pašdizainēts alus</strong></td>';
foreach($form->data as $key => $value) {
if($key === 'pasdiz_alus')
echo '<td style="width: 100px;">';
echo $form->data['pasdiz_alus'];
echo '</td>';
}
echo '<td style="width: 100px;">Cena šeit</td></tr>';
}
?>
</tbody>
</table>
And this is the output display, in this case the value of 'pasdiz_alus' is 54
The first row is the "wrong" one that I need to get rid off, and the second row is the "right" one.
5454545454545454545454545454545454545454545454545454545454
Bilde šeit Pašdizainēts alus 54 Cena šeit
Thanks for helping!
Brgds, Raivis

The problem in your script is here:
if($key === 'pasdiz_alus') // <-- missing opening "{"
echo '<td style="width: 100px;">'; // <-- inside the "if"
echo $form->data['pasdiz_alus']; // <-- OUTSIDE the "if"
echo '</td>'; // <-- OUTSIDE the "if"
} // <-- this matches the foreach "{"
Why do you cycle all the array keys, instead of directly accessing it?
<table style="width: 800px;">
<tbody>
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
<?php } ?>
</tbody>
</table>
This should solve your problem, but I'd recommend also to move the if part before even opening the table:
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<table style="width: 800px;">
<tbody>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
</tbody>
</table>
<?php } ?>

foreach($array as $k => $v)
{
if($k == 'pasdiz_alus' && $v > 0)
{
echo $v;
}
}
you need that addition in your if to make sure its only printed if it is the right key and its greater 0

Related

get a hidden table to appear with with a click with php

Im creating a simple School grade system using only php and I'm stumped of trying to figure out how to get a hidden table to appear below the main table when click on a "durchschnitt number"
Below is the code for the main table
<?php
if (!isset ($_SESSION['Saved_contacts']))
$Kontakte = array (
array ("Hr.", "Fruehauf", "Dennis", "13.02.2002", "Brucknerweg 34", 5212, "Hausen", '<u>3.6</u>'),
array ("Fr.", "Kaufmann", "Katharina", "04.03.2002", "Neubertbogen 24", 1680, "Romont", "Durchschnitt"),
array ("Hr.", "Fiedler", "Marcel", "08.16.2002", "Via Stazione 98", 8143, "Stallikon", "Durchschinitt"),
array ("Hr.", "Oster", "Tim", "08.26.2002", "Via delle Vigne 98", 1773, "Vuaty", "Durchschinitt"),
array ("Fr.", "Eichelberger", "Tanja", "07.22.2002", "Semperweg 6", 4223, "Blauen", "Durchschinitt"));
else
$Kontakte = $_SESSION['Saved_contacts'];
?>
<div style="width: 80%; min-width: 550px">
<h2>Kontakt des Schülers ...</h2>
<table>
<tr> <th>Nr.</th> <th>Anrede</th> <th>Name</th> <th>Vorname</th> <th>Geburtsdatum</th> <th>Adresse</th> <th>PLZ</th> <th>Ort</th> <th>Durchschnitt</th> </tr>
<?php
for ($i=0; $i < count($Kontakte); $i++) {
echo "<tr> <td><em>".($i+1)."</em></td>" . "<td style='text-align: center'>".$Kontakte[$i][0]."</td>" .
"<td>".$Kontakte[$i][1]."</td>" . "<td>".$Kontakte[$i][2]."</td>" . "<td>".$Kontakte[$i][3]."</td>" .
"<td>".$Kontakte[$i][4]."</td>" . "<td>".$Kontakte[$i][5]."</td>" . "<td>".$Kontakte[$i][6]."</td>" . " <td>".$Kontakte[$i][7]."</td
" . " <td><</tr>";
}
?>
</table>
As you can see in the first aray on the last line i made a link so that i'm able to click it.
Below is the hidden table I want to hide and reappear
<div class="Note">
<div style="width: 80%; min-width: 550px">
<table class="grade_Fruehauf" style="">
<tr>
<th>Fruehauf</th>
</tr>
<tr>
<th>Deutsch</th>
<th>3.5</th>
</tr>
<tr>
<th>Math</th>
<th>3.5</th>
</tr>
<tr>
<th>Biologie</th>
<th>3.5</th>
</tr>
<tr>
<th>Französisch</th>
<th>4</th>
</tr>
<tr>
<th>Durchschnitt</th>
<th style="border-top:solid;">3.6<th>
</tr>
</table>
<div>
</div>
Appreciate your help :)
If you want to use PHP only then you can use session, on clicking link set a session variable in a different file and redirect back, check that session variable and show/hide w.r.t it. (though JavaScript is preferable)
It's better to use javascript.
You just need to add the onlick attribute on the "durchschnitt number" td like
<td onclick="show('tableid');"></td>
and specify an id to the div or the table (es.: <table class="grade_Fruehauf" style="" id="example">.
Then you need a javascript code like this:
<script>
function show(tableid){
var x=document.getElementById(tableid);
if (window.getComputedStyle(x).visibility === "hidden") {
x.style.visibility = "visible";
}else{
x.style.visibility = "hidden";
}
}
</script>

PHP table, to add, remove and edit rows

I have issues with my table, I cannot use Javascript (which I know would be easier). I can only use HTML, PHP and CSS. This is my code that I currently have. The issues I need to resolve are the following:
I am able to add rows, delete and I am so also able to edit them by using the "contenteditable", however my issue is that every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Also if there is a way to have an edit button instead of my conteneditable method.
Here is my code:
input {
display: block; /* makes one <input> per line */
width: 150px;
}
<?php
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
if (isset($_REQUEST['count'])) $count = $_REQUEST['count'] + 1;
// set value for first load
else $count = 1;
}
if( isset( $_REQUEST["btnremove"]) == "REMOVE") {
// decrement the row counter
$count = $_REQUEST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<form name="form1">
<table class="table table-bordered table-striped table-hover text-center" align='center'>
<tr>
<th align="center">Name</th>
<th>Start </th>
<th>Size</th>
<th>First Condition</th>
<th>Second Conditon</th>
<th><input type="submit" name="btnadd" id="btnadd" value="ADD" align='center'></th>
</tr>
<?php
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td> <input type="submit" name="btnremove" id="btnremove" value="REMOVE"></td>
</tr>
';
}
?>
</table>
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>
every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Without Javascript you can't.
if there is a way to have an edit button instead of my conteneditable method
It's rather hard to suggest what your code should be since you don't ofer a description of what the code is intended to do, other than the code which doesn't do what you want. I think you mean something like:
<?php
$width=5;
$data=isset($_REQUEST['data']) ? $_REQUEST['data'] : array();
$count=count($data);
if (isset($_REQUEST['delete'])) {
foreach ($_REQUEST['delete'] as $i) {
unset($data[$i]);
}
}
$data=array_values($data);
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
$data[]=array();
}
...
foreach ($data as $i=>$row) {
print "<tr>\n";
for ($x=0; $x<$width; $x++) {
#print "<td><input type='text' name='data[$i][$x]'></td>\n";
}
print "<td><input type='checkbox' name='delete[]' value='$i'>";
print "</tr>\n";
}
print "<tr>
<td colspan='$width'>
<input type="submit" name="btnadd" id="btnadd" value="Add">
</td>
</tr>\n";

If condition within foreach

a FOREACH Loop outputs data from a booking array. Within this loop an IF condition filters the several bookings. If the booking date is after today, it outputs the booking information into a table. Works properly.
If there is no booking in future, there should a message printed "No bookings in future". In case that there is never made a booking, this works fine, but if bookings in past already exist, the message repeats for every booking in the past.
How can I prevent this?
Thanks in advance
Thorsten
<tbody>
<?php if ($tpl['booking_arr']) { ?>
<?php foreach ($tpl['booking_arr'] as $item) { ?>
<?php if (date('Ymd', strtotime($item['dt'])) > date('Ymd')) { ?>
<tr>
<td>
<?php echo date($tpl['option_arr']['o_date_format'], strtotime($item['dt']));?>
</td>
<td style="text-align: center;"><?php echo (int)$item['people'] + (int)$item['children']; ?></td>
</tr><?php } else { ?>
<tr>
<td colspan="2" style="font-size: 10px;">
keine bevorstehenden Reservierungen
</td>
</tr>
<?php } } } ?>
<tr><td style="border-bottom: 1px solid #000;" colspan="2"></td></tr>
<tr>
<td class="reg-seit">REG: <?php echo date($tpl['option_arr']['o_date_format'], strtotime($tpl['arr']['created']));?></td>
<td class="res-seit">RES: <?php $array = $tpl['booking_arr']; $getCount = count($array); echo ($getCount);?> </td>
</tr>
I don't know much about PHP but here's an idea: don't put the "no upcoming bookings" message inside the foreach loop without having some way to break from the loop (provided it is ordered by date). Use a boolean to check if there are any future bookings, which is set to false by default, and set to true if a future booking is found. After the loop, print the message if there are no future bookings. In pseudocode,
boolean futureBookingsExist = false;
foreach (date in bookingsArray) {
if (date > currentDate) {
outputBookingDate();
futureBookingsExist = true;
}
}
if (!futureBookingsExist) {
print("no future bookings exist");
}
You can just break out of the foreach block once you print out that message with break. This is based on the assumption that the content of $tpl['booking_arr'] is ordered by date. In case it's not, you will have to sort it.

How Can I nest two foreach to get right values output?

I have two foreach blocks to get some values from a web page (I'm scraping values from an HTML table).
<?php
include('../simple_html_dom.php');
$html = file_get_html('http://www.betexplorer.com/soccer/belgium/jupiler-league/results/');
foreach($html->find('td') as $e) {
echo $e->innertext . '<br>';
}
foreach( $html->find('td[data-odd]') as $td ) {
echo $td->attr['data-odd'].PHP_EOL;
}
?>
and this is my HTML code:
<tr class="strong">
<td class="first-cell tl">
Waasland-Beveren - Anderlecht
</td>
<td class="result">
1:0
</td>
<td class="odds best-betrate" data-odd="5.97"></td>
<td class="odds" data-odd="4.21"></td>
<td class="odds" data-odd="1.51"></td>
<td class="last-cell nobr date">21.02.2016</td>
</tr>
<tr class="">
<td class="first-cell tl">
Waregem - KV Mechelen
</td>
<td class="result">
2:3
</td>
<td class="odds" data-odd="1.83"></td><td class="odds" data-odd="3.71"></td>
<td class="odds best-betrate" data-odd="3.99"></td>
<td class="last-cell nobr date">21.02.2016</td>
</tr>
In this way, in my output, I get before values from the first foreach and, after, values from the second. I'd like to get values together in the right order. For example:
21.02.2016 Waasland-Beveren - Anderlecht 1:0 5.96 4.20 1.51
21.02.2016 Waregem - KV Mechelen 2:3 1.83 3.71 3.98
If they have the exact (count) or (length). Use the normal for after you assign them to two variables.
$td = $html->find('td');
$attr = $html->find('td[data-odd]');
for($i=0; $i < count($td); $i++)
echo $td[$i]->innertext."<br/>".$attr[$i]->attr['data-odd'].PHP_EOL;
Update:
You want to reorder the tds you received from the HTML file, that means you have to think of another logic in how to retrieve them. This updated code is very specific to your case:
$match_dates = $html->find("td[class=last-cell nobr date]"); // we have 1 per match
$titles = $html->find("td[class=first-cell tl]"); // 1 per match
$results = $html->find("td[class=result]"); // 1
$best_bets = $html->find("td[class=odds best-betrate]"); // 1
$odds = $html->find("td[class=odds]"); // 2
// Now to output everything in whatever order you want:
$c=0; $b=0; // two counters
foreach($titles as $match)
echo $match_dates[$c]->innertext." - ".$match->innertext." [".$results[$c]->innertext."] - Best bet rate: ".$best_bets[$c++]->attr['data-odd']." - odds: ".$odds[$b++]->attr['data-odd'].", ".$odds[$b++]->attr['data-odd']."<br/>";

issue to pass w3c validator [duplicate]

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>.

Categories