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);
Related
$result = mysqli_query($con, "SELECT * FROM users");
$usersArray=[];
tableArrayPushData($result, $usersArray);
function tableArrayPushData($result, $tableArray){
while ($row = $result->fetch_assoc()) {
$str = '';
foreach ($row as $value) {
$str = $str.$value.'|';
}
$newStr = rtrim($str, "| ");
array_push($tableArray,$newStr);
}
}
for ($i=0; $i<count($usersArray); $i++){//array is always empty at this point
echo "Ok";
echo "<br>";
}
I don't understand why, but usersArray is empty despite the fact that I added data there.
The MySQL table has rows with data, so it can't be empty.
You should use the & operator to allow the function to access the outer variable, like this:
function tableArrayPushData($result, &$tableArray) {}
Or use return.
The following code returns the field names of a result set. I also want it to return the values. How can I do this?
while ($row = mysqli_fetch_assoc($result)) {
foreach( $row as $field => $name) {
echo $field."<br>";
}
}
If we assume that your array looks like this:
$row["first_name"] = "John";
$row["last_name"] = "Doe";
$row["username"] = "john.doe";
Using this code:
while ($row = mysqli_fetch_assoc($result)) {
foreach( $row as $field => $value) {
echo "{$field} - {$value}<br>";
}
}
You will get an output like this:
first_name - John
last_name - Doe
username - john.doe
When you iterate through an array, using => operator, you are iterating in a "key-value" pair style. Every iteration holds the key and value as you can see.
Take a look at foreach for more information.
you are getting the value in $name variable
foreach( $row as $field => $name) {
echo $field . " = " . $name . "<br>";
}
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 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>';
}
$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
}