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>";
Related
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>";
?>
I have the following array:
$array=array("string",array(1,2,3),true,"php");
and I want to print indexes like:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
<?php
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$value)
{
if(is_array($value))
{
foreach($value as $childkey=>$childvalue)
{
echo $key . "." . $childkey . "=>" . $childvalue . "\n";
}
}
elseif(is_bool($value))
{
echo $key . "=>" . ($value ? "true" : "false") . "\n";
}
else
{
echo $key . "=>" . $value . "\n";
}
}
Output:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
Try this
<?php
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$value){
if(is_array($value)){
foreach($value as $key1=>$value1){
echo $key.".".$key1." => ".$value1."</br>";
}
}
else{
echo $key." => ".$value."</br>";
}
}
<?php
$array=array("string",array(1,2,3),"true","php");
foreach($array as $key=>$value){
if(is_array($value)){
foreach($value as $key1=>$loop){
echo $key.'.'.$key1 .'=>'.$loop."<br>";
}
}else{
echo $key .'=>'.$value."<br>";
}
}
?>
Try this code
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$val){
if(is_array($val)){
foreach($val as $key1=>$val1){
echo $key.'.'.$key1 .'=>'.$val1.'<br/>';
}
}else{
echo $key .'=>'.$val.'<br/>';
}
}
You can try this-
<?php
$arr=array("string",array(1,2,3),true,"php");
$res=convArray($arr);
foreach($res as $k=>$v){
echo $k."=>".$v."\n";
}
function convArray($arr)
{
foreach($arr as $k1=>$v1){
if(is_array($v1)){
foreach($v1 as $k2=>$v2){
$res[$k1.'.'.$k2]=$v2;
}
}else{
$res[$k1]=$v1;
}
}
return $res;
}
?>
Try this:
<?php
$array=array("string",array(1,2,3,array('a','b','c')),true,"php");
$kt = array();
function showarray($arr,$k) {
global $kt;
foreach($arr as $key => $v) {
$nk = $k == '' ? $key:$k.'.'.$key;
if(is_array($v)) {
showarray($v,$nk);
} else {
$kt[$nk] = $v;
}
}
}
showarray($array,"");
print_r($kt);
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.
I had a really long description written for my problem, but I've progressed and have managed to get it partially working... I basically want every other row in a table to be a different color - and to highlight certain rows if they meet a condition that I set.
foreach ($data as $row) {
$style = null;
while (($values = fgetcsv($handle, 0, '|')) !== false) {
$comment_lines = $values[6];
$priority = $values[7];
$time_worked = $values[11];
$var_X = strpos($priority, '1');
$var_C1 = strpos($comment_lines, 'CCB');
$var_C2 = strpos($comment_lines, 'CEB');
$var_U = empty($time_worked);
}
if (empty($values[0]) && count($values) === 1) {
continue;
}
if (strlen($var_X)) {
echo '<tr class="gradeX">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_C1 !== false) {
echo '<tr class="gradeC">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_C2 !== false) {
echo '<tr class="gradeC">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_U !== false) {
echo '<tr class="gradeU">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($odd) {
$odd = !$odd;
echo '<tr class="even gradeA">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else {
$odd = !$odd;
echo '<tr class="odd gradeA">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
}
echo '</tr>';
}
Right now, only the 'every other' is working. I can't get the gradeX, gradeC and gradeU to show anything...
EDIT:
This is the working code, cheers.
while (($values = fgetcsv($handle, 0, '|')) !== false) {
$style = null;
$comment_lines = $values[6];
$priority = $values[7];
$time_worked = $values[11];
$var_X = strpos($priority, '1');
$var_C1 = strpos($comment_lines, 'CCB');
$var_C2 = strpos($comment_lines, 'CEB');
$var_U = empty($time_worked);
if (empty($values[0]) && count($values) === 1) {
continue;
}
if ($var_X !==false) {
echo '<tr class="gradeX">';
} elseif ($var_C1 !== false) {
echo '<tr class="gradeC">';
} elseif ($var_C2 !== false) {
echo '<tr class="gradeC">';
} elseif ($var_U !== false) {
echo '<tr class="gradeU">';
} else {
//nothing
}
foreach ($values as $index => $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
In your loop you need to add a counter to identify whether you're currently on an 'even' or an 'odd' iteration, and then add either class 'odd' or 'even' into the as you output it.
There's a related question already on here that covers that here: php: how to add odd/even loop in array
The other parts should be fairly easy by using if statements to check if the relevant data exists or not.
For example, strlen will tell you whether a variable has length or not (to put out 'gradeX'), and strpos will allow you to check if a varibale contains a string (for 'gradeC').
If you can target IE later than IE8 and forget about IE8, you can use CSS3:
tr:nth-child(odd) {
background-color: #EEEEEE;
}
tr:nth-child(even) {
background-color: #FFFFFF;
}
This is what im using to loop through an array of arrays.
$csvpre = explode("###", $data);
$i = 0;
$bgc = 0;
foreach ( $csvpre AS $key => $value){
$info = explode("%%", $value);
$i++;
if($i == "1"){
echo "<tr bgcolor=#efefef><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
} else {
if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
echo "<tr $bgcgo><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
$bgc++;
}
}
How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array.
Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>
You mean like this:
<?php
.......
echo "<tr $bgcgo><td></td>";
$j = 0; //you need a counter
foreach ( $info as $key => $value ) {
$j++;
if ($j != 30) {
echo "<td>$value</td>";
} else {
echo "<textarea>$value</textarea>";
}
}
echo "</tr>";