How do I return the column name in mysqli by row?
...
$result = $mysqli->query("SELECT * FROM `xtdf` WHERE posterid='bike'");
$rows=mysqli_fetch_array($result);
foreach($rows as $column => $value) {
echo $column . " " . $value;
}
...
This code above will return all columns name and values, but I want only the column name by the value. I have tried this code below but returns nothing.
...
$result = $mysqli->query("SELECT * FROM `xtdf` WHERE posterid='bike'");
$rows=mysqli_fetch_array($result);
foreach($rows as $column => $value) {
if ($value == "bike") {
echo $column;
}
}
Edit: I don't want put the result in array, or bring all results. Only return one value.
PHP version: 5.5.15
What should I do?
This should do the trick
while($row=mysqli_fetch_array($result)) {
foreach($row as $column => $value) {
if ($value == "bike") {
echo $column;
}
}
}
You need to iterate through all values that you fetch from database.
Try This:
while($rows=mysqli_fetch_assoc($result)){
foreach($rows as $column => $value) {
if ($value == "bike") {
echo $column;
}
}
}
good luck
Related
I have the following array:
$array = [
['2017-02-26', '2017-02-27'],
['2017-03-01'],
['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
['2017-01-05', '2017-01-06', '2017-01-07']
];
I'm looking to loop into this array to have something like this:
// When several dates
From 2017-02-26 to 2017-02-27.
// When only one date
On the 2017-03-01.
What I tried:
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
echo "On the $key[$value]";
}
else {
$first = reset($array);
$last = end($array);
echo "From ".$first." to ".$last.;
}
}
But it doesn't work when there is only one date in the row.
You are looping by foreach() so it will display last echo string .Store result to one variable Eg($display) will be more easy to display that
$display = "";
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
$display .= "On the $value[0] <br>";
}
else {
$first = $value[0];
$last = $value[$count-1];
$display .= "From ".$first." to ".$last."<br>";
}
}
echo $display;
Try this:-
foreach ($array as $key => $value) {
$count = count($value);
if($count==1) {
echo "On the ".$value[0];
}
else {
$first = reset($value);
$last = end($value);
echo "From ".$first." to ".$last;
}
}
Or just copy paste this code, it will work. Your main inside array to play with is $value.
let have a look in my script. below i am getting no values in while i am assigning the value to variable $specdata
$result = mysqli_query($connection, "select * from productrates");
if (mysqli_num_rows($result) > 0)
{
echo "<div class='box-body'>";
$header='';
$rowdata='';
$header='<tr>';
$field= mysqli_fetch_fields($result);
foreach ($field as $val) {
if($val->name !="specs")
{
$header.= "<th style='background:#E1E1E1'>".$val->name."</th>";
}
}
$header.='</tr>';
while ($row = mysqli_fetch_array($result))
{
$field= mysqli_fetch_fields($result);
$rowdata.="<tr class='linehover'>";
// in this section i want to show $specdata values in div id="specs"
foreach ($field as $val)
{
$specdata ='';
if($val->name =="specs")
{
$specdata = $row[1];
echo $specdata;echo "<hr>";
}
if($val->name !="specs")
{
if($val->name == "Product")
{
$rowdata.= "<td class='nohover'>".$row[$val->name]."<div id='specs'>".$specdata."</div></td>";
}
else
{
$rowdata.= "<td>".$row[$val->name]."</td>";
}
}
}
$rowdata.='</tr>';
}
}
mysqli_free_result($result);
echo '<table class="table table-bordered" id="pricelist">'.$header.$rowdata.'</table></div>';
// but every i am getting null value in that div
Let's chop the unimportant bits out and look at this
foreach ($field as $val)
{
$specdata ='';
if($val->name =="specs")
{
$specdata = $row[1]; // no clue what $row[1] holds
echo $specdata; echo "<hr>";
}
if($val->name !="specs")
{
echo $specdata; // $specdata = '';
}
}
Remember, $specdata is being set to an empty string every time the loop starts over. So even if you set it to $row[1] this time, it will be an empty string on the next iteration. So if($val->name !="specs") will always give you an empty string.
Have you checked that the value of '$val->name'? because if it is different from "specs"
it will not pass on your first condition:
if($val->name =="specs")
just will not fill the variable $specdata
just to give you a hint it might be better to build this code to switch case like:
foreach ($field as $val)
{
$specdata = '';
switch ($val->name) {
case "specs":
$specdata = $row[1];
echo $specdata;echo "<hr>";
break;
case "Product":
$rowdata.= "<td class='nohover'>".$row[$val->name]."<div id='specs'>".$specdata."</div></td>";
break;
default:
$rowdata.= "<td>".$row[$val->name]."</td>";
}
Here is the content of my table:
How to count all names in different table cells to get a result like this?
Bob-7
Alex-3
Ivan-5
Nina-5
.........
With this code:
<?php
$q= mysqli_query($db, 'SELECT * FROM names');
while ($all = mysqli_fetch_array($q)) {
$k1= $all['Name1'];
$k2= $all['Name2'];
$k3= $all['Name3'];
$k4= $all['Name4'];
$k5= $all['Name5'];
$k6= $all['Name6'];
$arr[]= $k1;
$total_values = array_count_values($arr);
}
foreach ($total_values as $key => $value) {
echo $key .'-'. $value .'<br>';
}
My output result is:
Bob-3
Alex-1
Ivan-1
Nina-2
When I change my code to:
<?php
$q= mysqli_query($db, 'SELECT * FROM names');
while ($all = mysqli_fetch_array($q)) {
$k1= $all['Name1'];
$k2= $all['Name2'];
$k3= $all['Name3'];
$k4= $all['Name4'];
$k5= $all['Name5'];
$k6= $all['Name6'];
$arr=array($k1, $k2);
$total_values = array_count_values($arr);
foreach ($total_values as $key => $value) {
echo $key .'-'. $value .'<br>';
}
}
My output result is:
Bob-1
Alex-1
Ivan-1
Nina-1
Nina-1
Bob-1
Bob-1
Ivan-1
Nina-1
Nina-1
.......
What is wrong and what I have to do to add $k2, $k3....$k6 in the array $arr?
This will count the number of occurences for a single name:
$q= mysqli_query($db, 'SELECT * FROM names');
$arr = array();
// 1) loop through rows
while ($all = mysqli_fetch_array($q, MYSQLI_NUM)) {
// 2) loop through cells in a row
foreach($all as $val) {
// 3) 'roll out' the values into a one-dimensional array
if(empty($val)) { continue; } // if you don't want to count empty cells
$arr[] = $val;
}
}
// 4) count the number of occurences
$names_qty = array_count_values($arr);
// optional loop that shows the results
foreach($names_qty as $name=>$qty) {
echo $name.'-'.$qty.'<br />';
}
The benefit of this solution is that you call the counting function only once and not in every iteration.
You have to accumulate data in a loop and print it after all data is processed.
$total = array();
$result = mysqli_query($db, 'SELECT * FROM names', MYSQLI_USE_RESULT);
while (mysqli_fetch_assoc($result) as $row) {
foreach ($row as $col => $value)
total[$col]++;
}
}
print_r($total);
This code is tested and should do the trick.
<?php
$arr = array();
$q = mysqli_query($db, 'SELECT * FROM names');
while ($all = mysqli_fetch_assoc($q)) {
foreach($all as $val) {
if(empty($val)) {
continue;
}
// saves all names with the name as key
$arr[$val][] = $val;
}
}
// Output
foreach($arr as $k => $v) {
// count how many times the name was pushed into the array
echo $k.' - '. count($v);
}
I have a foreach to loop through the data provided by a PDO SQL query:
foreach ($team as $row){
$count++;
$teamNumber = 'team'.$count;
if ($currentScores[0]['team'.$count] == ""){
$red = "red";
}
echo "<strong><font color='".$red."'>".$row['name']."</font></strong>";
echo $currentScores[0]['team'.$count];
if ($count < 2)
echo " vs ";
}
Now, I want to loop again through the $team array but it just returns the last value of the array during the second loop.
I tried the same thing:
foreach ($team as $row) {
.....
.......
}
How could I run again through the array?
Simple enough, just do a foreach loop on $row as you have previously.
foreach($array as $key => $val) {
foreach($val as $a => $b) {
echo $b;
}
}
I have a while loop fetching a row and I'm trying to create a structure so that each row will create 2 links. The first is a German word and the second being an English one. The output I'm getting is repeated as if the row isn't being incremented. I've narrowed it down to this:
PHP:
while ($row = $database->row()->fetch()) {
foreach ($row as $value) {
$this->data .= $value . "!";
}
list($this->pid, $this->german, $this->english) = explode("!", $this->data);
$this->links .= "$this->german<br/>$this->english<br/>";
}
Output:
die Männer
men
die Männer
men
$row = array();
while ($row = $database->row()->fetch()) {
$row[] = $row;
}
foreach ($row as $value)
{
$this->data .= $value . "!";
list($this->pid, $this->german, $this->english) = explode("!", $this->data);
$this->links .= "$this->german<br/>$this->english<br/>";
}
I've solved the problem by looking at the keys and creating variables based on a simple matched string test. If anyone can improve my answer I would like to hear it. Thanks.
PHP:
while ($row = $database->row()->fetch()) {
while(list($key, $value) = each($row)) {
if ($key == "PID") {
$this->pid = $value;
}
if ($key == "german") {
$this->german = $value;
}
if ($key == "english") {
$this->english = $value;
}
}
$this->links .= "$this->german<br/>$this->english<br/>";
}