checkbox returning last value - php

I´m trying to do a checkbox in my form but its just returning the last value. What´s wrong here?
if ($type == "w") {
//check buttons
foreach ($values as $indexa => $value) {
$value = trim($value);
echo "<input type=\"checkbox\" name=\"input[$fieldno]\" value=\"$value\" ";
if (isset($input)) {
if ($input[$fieldno] == $value) {
echo " checked";
// var_dump($fieldno); die();
}
}
if ($compulsory == 1) {
echo " class=\"required\"";
}
$label = ($value == 'OTHER') ? 'OUTROS' : $value;
echo " onclick=radio_other($fieldno,\"$value\")> $label<br>";
}
if (strtoupper($value) == "OTHER") {
echo "<input type=\"text\" name=\"other[$fieldno]\" id=\"other[$fieldno]\" style=\"display:none\">";
}
}
Ps: $type == w is a checkbox type.

Related

how can get a field with label as date in php

<?php
$count = 1;
foreach($_SESSION["searchmultipleTSIKDO"] as $key=>$rows) {
echo '<tr><td>'.$count++.'.</td>';
echo '<td>'.$rows["key"].'</td>';
echo '<td>';
if ( $rows["key"] == 'bcounty') {
$array = $listcounty->get("WHERE id = $rows[value]");
foreach($array as $rows1) {
echo $rows1["name"];
}
}
else if ( $rows["key"] == 'gender') {
if ( $rows["value"] == 1) {
echo 'Male';
} else {
echo 'Female';
}
}
else if ( $rows["key"] == 'dob') {
.......
}
}
else if ( $rows["key"] == 'qualification') {
$array = $listqualification->get("WHERE id = $rows[value]");
foreach($array as $rows1) {
echo $rows1["name"];
}
}
else {
echo $rows["value"];
}
echo '</td>';
echo "<td><a href=\"report.php?deletesearchmultiple=$key\" style='color: red;'> X</a></td>";
}
?>
You mean something like
else if ( $rows["key"] == 'dob') {
echo '<label>DOB: <input type="date" value="'.date("Y-m-d\TH:i:s",$rows["value"]).'" /></label>';
}

PHP--How do I put form information into an HTML table once it has been saved to a text file?

This is just a block of code I am struggling with. Not sure if I am correct while trying to get this to format into an HTML table. I've messed with this for the last two hours trying to get it to work, I still don't have a clue what I am doing wrong. I am trying to get the information that comes from my array posted into the HTML table.
$ExistingSig = array('Name'=>'1','Version'=>'2','Hardware'=>'3', 'System'=>'4', 'Frequency'=>'5', 'Solution'=>'6');
$Report = array('Name'=>'a','Version'=>'b','Hardware'=>'c', 'System'=>'d', 'Frequency'=>'e', 'Solution'=>'f');
echo "<table border='1'>";
$count1=0;
foreach ($ExistingSig as $key => $value)
{
echo "<tr>";
if(!is_array($value))
{
if($key == 'Name' or $key == 'Version' or $key == 'Hardware' or $key == 'System' or $key == 'Frequency' or $key == 'Solution')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
$count2=0;
foreach ($ExistingSig as $key => $value)
{
if($count1==$count2){
//to eliminate array to string conversion error
if(!is_array($value))
{
if($key == '$AddName' or $key == '$AddVer' or $key == '$AddHard' or $key == '$AddSys' or $key == '$AddFreq' or $key == '$AddSol')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
}
$count2++;
}
echo "</tr>";
$count1++;
}
echo "</table>";
See if this works for you
<?php
$ExistingSig = array('Name'=>'1','Version'=>'2','Hardware'=>'3', 'System'=>'4', 'Frequency'=>'5', 'Solution'=>'6');
$Report = array('Name'=>'a','Version'=>'b','Hardware'=>'c', 'System'=>'d', 'Frequency'=>'e', 'Solution'=>'f');
$wantedKeys = array('Name','Version','Hardware','System','Frequency', 'Solution');
$thead = "<thead>";
$tbody="<tbody>";
foreach ($ExistingSig as $key => $value){
if(in_array($key,$wantedKeys)){
$thead .= "<th>$key</th>";
$tbody .= "<td>$value</td>";
}
}
echo "<table border='1'>$thead</thead>$tbody</tbody></table>";
?>

PHP if or statement not working as expected

I am trying to loop through my $_POST variables and display data that has key = lap* only. This is the code I have so far.
foreach($_POST as $key=>$val)
{
if($key != "time" || $key != "avgspd" || $key != "kartno")
{
echo $key . "<br>";
}
}
The result it gives me is this
time
lap1
lap2
lap3
lap4
lap5
lap6
lap7
lap8
lap9
lap10
lap11
lap12
lap13
lap14
lap15
lap16
lap17
avgspd
kartno
As you can see, it still displays the keys avgspd,kartno and time. The strange and confusing this is if I change my code to this:
foreach($_POST as $key=>$val)
{
if($key == "time" || $key == "avgspd" || $key == "kartno")
{
echo $key . "<br>";
}
}
I get this as the result:
time
avgspd
kartno
This doesn't make any sense. If I check if the key is not equal to time,avgspd or kartno it displays all keys, yet when I say for it to only display keys "time","avgspd" or "kartno" it works as expected! Whats going on here? What do I need to do to fix it. Is some earlier code causing the error? If so here is my code.
Index.php
<?php
if(!$_POST)
{
include("functions.php");
echo "<link rel='stylesheet' type='text/css' href='style.css'>";
echo "<form action='' method='POST'>";
echo "<table><tr>";
table_head("Time","lap 1","lap 2","lap 3","lap 4","lap 5","lap 6","lap 7","lap 8","lap 9","lap 10","lap 11","lap 12","lap 13","lap 14","lap 15","lap 16","lap 17","Avg Spd");
echo "</tr><tr>";
display_fields();
echo "</tr></table>";
echo "Kart Number: <input type='text' size='2' name='kartno'/><br>";
echo "<input type='submit'/>";
echo "</form>";
} else {
foreach($_POST as $key=>$val)
{
if($key == "time" || $key == "avgspd" || $key == "kartno")
{
echo $key . "<br>";
}
}
}
?>
functions.php
<?php
function table_head()
{
if ( func_num_args() > 0 )
{
$args = func_get_args();
foreach($args as $value)
{
echo "<th>" . $value . "</th>";
}
}
}
function display_fields()
{
$i = 0;
$a = 19;
$name="hi";
while($i++ < $a)
{
array_push($numbers,$i);
if($i==1)
{
$name = "time";
} else if($i > 1 && $i < 19){
$name = "lap" . strval($i-1);
} else {
$name = "avgspd";
}
echo "<td><input type='text' size='8' name='" . $name . "'/></td>";
}
}
?>
I believe you are looking for &&. Or else I'm missing something.
foreach($_POST as $key=>$val)
{
if($key != "time" && $key != "avgspd" && $key != "kartno")
{
echo $key . "<br>";
}
}
You want all three conditions to be met in order to print.
Edit: Even better! Check if it starts with lap. This is more appropriate for what you want.
foreach($_POST as $key=>$val)
{
if(substr($key, 0, 3) == "lap")
{
echo $key . "<br>";
}
}
This is always true:
if($key != "time" || $key != "avgspd" || $key != "kartno")
What you mean is:
if($key != "time" and $key != "avgspd" and $key != "kartno")
You can rewrite it to:
if(! in_array($key,array("time","avgspd","kartno")))
Here you go:
foreach($_POST as $key=>$val)
{
if($key != "time" && $key != "avgspd" && $key != "kartno")
{
echo $key . "<br>";
}
}
Explaination: If you put the OR statement here, it will always print because you basically told your program to print it if it is different from "time" OR different from "avgspd". And "time" is different from "avgspd" so it prints.

Display array values in a table

I have the following code, which currently displays the array values in resArr and resArr2 separately. I would like to display this data in a table, showing the value of array resArr in column 1 and the value of resArr2 in column 2. I have tried experimenting with tr and td however for some reason this has no effect on the output.
Could anyone provide me with any possible solution please?
$resArr = json_decode($res, 1);
$resArr2 = json_decode($res2, 1);
foreach ($resArr as $key => $value)
{
echo "<tr>";
if(!is_array($value))
{
if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
echo "</tr>";
}
foreach ($resArr2 as $key => $value)
{
//to eliminate array to string conversion error
if(!is_array($value))
{
if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
{
echo $key . ':' . $value;
echo "<br />\n";
}
}
}
I belive you want something like this:
$resArr = json_decode($res, 1);
$resArr2 = json_decode($res2, 1);
$labels = array('attire','category','location','name','website','checkins','likes');
$count1=0;
echo '<table border="1">';
echo '<tr><th>key</th><th>resArrLabel</th><th>resArr2Label</th></tr>';
foreach ($labels as $label)
{
echo '<tr>';
echo '<th>'.$label.'</th>';
echo '<td>'.(array_key_exists($label, $resArr) ? $resArr[$label] : '').'</td>';
echo '<td>'.(array_key_exists($label, $resArr2) ? $resArr2[$label] : '').'</td>';
echo '</tr>';
}
echo '</table>';
It was much faster becouse use only one loop and add th label to each column and row. Try to add more css style.
You have to iterate both arrays at the same time. Try this
$resArr = json_decode($res, 1);
$resArr2 = json_decode($res2, 1);
$count1=0;
echo "<table border='1'>";
foreach ($resArr as $key => $value)
{
echo "<tr>";
if(!is_array($value))
{
if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
$count2=0;
foreach ($resArr2 as $key => $value)
{
if($count1==$count2){
//to eliminate array to string conversion error
if(!is_array($value))
{
if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
}
$count2++;
}
echo "</tr>";
$count1++;
}
echo "</table>";
Look at the results displayed for me. I am using sample array.
$resArr = array('attire'=>'1','category'=>'2','location'=>'3');
$resArr2 = array('attire'=>'a','category'=>'b','location'=>'c');
echo "<table border='1'>";
$count1=0;
foreach ($resArr as $key => $value)
{
echo "<tr>";
if(!is_array($value))
{
if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
$count2=0;
foreach ($resArr2 as $key => $value)
{
if($count1==$count2){
//to eliminate array to string conversion error
if(!is_array($value))
{
if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
}
$count2++;
}
echo "</tr>";
$count1++;
}
echo "</table>";

How to put string variable at mysql_resutl field?

I want to run this code :
if ($_GET['CHOOSE'] == 1)
{
$field = " 'col1' ";
}
else
{
$field = " 'col2 ' ";
}
echo mysql_result($result,0,$field);
unfortunately I got false ??
Try this
if ($_GET['CHOOSE'] == 1)
{
$field = "col1";
}
else
{
$field = "col2";
}
echo mysql_result($result,0,$field);

Categories