I am building a table in which i should put some values inside the <td></td>.
What I have is a $checked value which I convert it in some string like: id1,id2,id3
What I need to do is a loop which permits me to print this values like this. Something like this:
(for i=0, i<length of words, i++) {
echo "<td> .id[i].</td>"
}
My real variable is this:
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked = implode(',', $_POST['checkbox']);
echo $checked;
Thanks!
If $_POST['checkbox'] is already an array then you don't have to implode() it before processing :
foreach ($_POST['checkbox'] as $id) {
echo '<td>' . $id . '</td>';
}
Related
I have this json file: and I would like to get all the title so my code is: var_dump($json['results'][0]['title']); but it getting just one title, I know I need to do a foreach but I don't know how :( so if you could help me that will be great ! Thanks
Your data is coming back as an array with item's inside of it. This mean's you'll need to loop through this JSON and print each item;
foreach($json['results'] as $movie) {
echo $movie['title'] . "<br />";
}
You will want to loop through all of the JSON results objects and echo or store the 'title' value;
foreach ($json['results'] as $object) {
//Option 1
echo $object['title'];
//Option 2
array_push($titles, $object['title'];
}
Assuming that the variable $json contains your data, I see two equivalent options in order to iterate trough the array
Using for
for($index = 0; $index < count($json['results']); $index++) {
echo $json['results'][$index]['title'] . "\n";
}
Using foreach
foreach($json['results'] as $movie) {
echo $movie['title'] . "\n";
}
I have a particular JSON file that looks like this:
[
{
"objID":"kc6BvvNlVW",
"string":"bill",
"createdOn":"2018-09-18T01:51:02",
"updatedOn":"2018-09-18T01:51:02",
"number":1,
"boolean":true,
"array":["item1","item2"],
"pointer":{"type":"__pointer","objID":"hYtr54Ds","className":"Users"}
},
{
"objID":"sS1IwFPPWh",
"string":"tom",
"createdOn":"2018-09-18T01:59:40",
"updatedOn":"2018-09-18T01:59:40",
"number":12.3,
"boolean":false,
"array":["item1","item2"],
"pointer":{"type":"__pointer","objID":"tRe4Fda5","className":"Users"}
}
]
1. I need to first check if the "pointer" object has "__pointer" inside the type key and show only the objID value in an HTML table, like this:
"tRe4Fda5"
Right now, this is how my table looks like:
And here's my foreach PHP code (into a table row):
foreach($jsonObjs as $i=>$obj) {
$row_id = $i;
echo '<tr>';
foreach($obj as $key => $value){
// $value is an Array:
if (is_array($value)) {
echo '<td>';
foreach($value as $k=>$v){
// $v is a Pointer
if ($v === '__pointer') {
echo json_encode($v); // <-- WHAT SHOULD I DO HERE ?
// $v is an Array:
} else {
echo json_encode($v);
}
}
echo '</td>';
// $value is a Number:
} else if (is_numeric($value)){
echo '<td>'.(float)$value.'</td>';
// $value is a String:
} else { echo '<td>'.$value.'</td>'; }
}
As you can see in the pointer column, the string I get is:
"__pointer""hYtr54Ds""Users"
with no commas as separators, so this is the line of code I need to edit:
echo json_encode($v); // <-- WHAT SHOULD I DO HERE ?
I've tried with echo json_encode($v[$k]['__ponter']);, but no positive results.
So my final first question is: how can I get each VALUE of the "pointer" array?
2. Also, the second row of the boolean column shows noting since its value is false, shouldn't it show 0, since the first row shows 1 (true)?
You can look into the object during the second loop to see if it has a property called type and if that property is set to __pointer.
foreach($jsonObjs as $i=>$obj) {
$row_id = $i;
foreach($obj as $key => $value){
// see if $value has a type property that is set to pointer
if (isset($value['type']) && $value['type'] == "__pointer") {
// $value is the pointer object. Do with it what you will
echo "<td>" . $value['objID'] . "</td>";
}
// more code
}
}
instead of
foreach($value as $k=>$v){
// $v is a Pointer
use
foreach($value as $k)
{
//then check for pointer
if($k->type === '__pointer')
{
echo json_decode($k); //here you will get proper key and value
}
}
Is it possible to loop a function variable from this? I am trying to loop the $item[$a-1]
echo load_func($hid, $item[$a-1]);
And make it something like this but I know this is wrong (just an idea):
echo load_func($hid, for($a=1;$a<=$addctr;$a++){$item[$a-1]});
This is the actual but fail because it loops the whole function.
echo "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
for($a=1;$a<=$addctr;$a++){
echo load_func($id, $item[$a-1]);
}
echo "</select>";
The purpose of the function is to automatically select an option based from the record saved on a table.
Try to pass the whole item to load_function();
echo load_func($hid, $item);
And deal with every item in the function itself.
function load_func($hid, $item) {
$return = "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
foreach ($item as $option) $return .= $option;
$return .= "</select>";
return $return;
}
From what I am understanding from your question, you should pass the array $addctr to the function. And inside the function you should put your for loop and do the calculations.
something like:
function load_func($id, $items) {
$strOptions = "";
for($a=1;$a<=$items;$a++){
if($items[$a-1] != $id)
$strOptions .= "<option>Your value</option>";
else
$strOptions .= "<option selected>Your value</option>";
}
return $strOptions;
}
I keep getting array to string conversion in this code.. please help me
$qty_parse = oci_parse($conn, 'select qty from master_drawing');
oci_execute($qty_parse);
echo "<tr>\n";
foreach ($row as $item)
{
//echo " <td>".($item !== null ? htmlentities($item, ENT_QUOTES):" ")."</td>\n";
echo " <td>".($item);
if (is_numeric($item)){
$quantity = oci_fetch_array($qty_parse, OCI_ASSOC);
echo '/'.$quantity.'<meter value=10 min="2" max="10"></meter>';
}else {
echo ' ';
}
}
Looks like oci_fetch_array() returns array (array in function name should tell you something ;)).
You can use var_dump($quantity); to see what was returned by this function.
I guess that what you need to do is something like this: echo '/'.$quantity['qty'].'<meter value=10 min="2" max="10"></meter>';
First of all, your $row variable is not defined. You can use next solution:
$qty_parse = oci_parse($conn, 'select qty from master_drawing');
oci_execute($qty_parse);
while ($item = oci_fetch_array($qty_parse, OCI_ASSOC))
{
echo " <td>".($item['qty']);
if (is_numeric($item['qty'])){
echo '/'.$item['qty'].'<meter value=10 min="2" max="10"></meter>';
}else {
echo ' ';
}
}
P.S. When oci getting associated array via OCI_ASSOC - your script gets $item variable like:
$item['qty'] = 'value';
If you want to get value from $item as string variable, redefine you variable on loop like:
$item = current($item);
$searchquery = mysql_query("SELECT * FROM regform_admin WHERE status = 'Approved' AND month LIKE '%".$checkmonth."%' AND day LIKE '%".$checkday."%' AND year LIKE '%".$checkyear."%' OR month2 LIKE '%".$checkmonth."%' AND day2 LIKE '%".$checkday."%' AND year2 LIKE '%".$checkyear."%' OR betday LIKE '%".$checkday."%'");
while($fetchres = mysql_fetch_array($searchquery)) {
$vehicles = $fetchres['vehicles'];
echo "<b>$vehicles</b><br>";
}
include "vehicledbconnect.php";
$searchquery2 = mysql_query("SELECT * FROM vehicletbl WHERE vehicle NOT LIKE '%".$vehicles."%'");
while($fetch = mysql_fetch_array($searchquery2)) {
$v = $fetch['vehicle'];
$vehed = $fetch['vehed']; $vehcap = $fetch['vehcap']; $vehyr = $fetch['vehyr'];
$type = $fetch['type'];
echo "<tr>";
echo "<td class=light><input type = 'checkbox' name='chk[]'></td>";
echo "<td class=light>$v</td>";
echo "<td class=light>$type</td>";
echo "<td class=light>$vehyr</td>";
echo "<td class=light>$vehed</td>";
echo "<td class=light>$vehcap</td>";
echo "<td class=light>$vehcolor</td>";
echo "</tr>";
echo "$array";
}
Is it possible to echo all values inside the while statement from $searchquery inside $searchquery2?
For example the values of $searchquery is:
vehicle1
vehicle2
vehicle3
Now I'm going to echo all of it inside $searchquery2, is it possible? I try to echo $vehicles (from $searchquery) inside $searchquery2 but it only echoes the last value (example. vehicle3) because I know it is not inside the while statement of $searchquery.
Is it possible? Thanks.
Build a loop in loop.
Try this:
mysql_query("SELECT * FROM regform_admin WHERE...")
while(...)
{
$vehicles = $fetchres['vehicles'];
echo "<b>$vehicles</b><br>";
mysql_query("SELECT * FROM vehicletbl WHERE vehicle NOT LIKE '%".$vehicles."%'");
while(...)
{
echo ...
}
}
Instead of echoing inside the while loop, try buffering the output to a variable instead:
$ob = '';
while($fetchres = mysql_fetch_array($searchquery))
$ob .= '<b>' . $fetchres['vehicles'] . '</b><br>';
echo($ob);
During your second while loop, $ob is still alive and you can echo it out just as easily. Here I'm storing it as a string, but you can store the values as an array also if you need more granular access to the elements.
Update
If you want to store them into an array, try it this way:
$ob = array();
while($fetchres = mysql_fetch_array($searchquery))
$ob[] = $fetchres['vehicle']; // This pushes the current vehicle onto the end of the array
After this loop, you just have the string for each vehicle in the array, and none of the formatting. You can add that in when you output the elements. Iterate over your array with foreach:
foreach($ob as $k => $v) {
// Inside this loop, $k is the key, or index, and $v is the value.
// You can change the names of these variables by modifying the line above, e.g.
// foreach($ob as $foo => $bar) <-- $foo = key/index, $bar = value
}