I have an array:-
$resolution = array();
foreach ($report as $key => $val) {
$queryToGetRes = "SELECT resolution,ticket_no FROM techzilla.bugs WHERE bug_id = '$val' ";
$sqlResult = mysql_query($queryToGetRes) or die (mysql_error());
while ($resolutionAns = mysql_fetch_array($sqlResult)) {
$resolution[$resolutionAns['resolution']][] = $resolutionAns['ticket_no'];
}
}
I need to print the array which stores the resolution and ticket no.
try something like this:
echo "<pre>" . print_r($resolution, true) . "</pre>";
For debuging purpuses you should use
var_dump($resolution);
If you want to use it
foreach ($resolution as $r => $ids){
echo "Resolution : $r\n";
foreach ($ids as $id) {
echo "$id\n";
}
}
Related
**
data did not insert into table, please help
I am not understand
which loop to use
I want to insert data into a table using loop I have Multi -D array
using PHP
**
<?php
$data = array('eid' => array(124,658,457,145),
'data' => array('2012/10/12','2012/10/15','2012/10/22','2012/10/02'),
'name' => array('Chiku','Lipu','Babu','Dipu')
);
foreach($data as $key => $value){ ///////////
$k = $key;
$v = $value;
echo "<pre>";
print_r ($k);
echo "<br>";
foreach($v as $ke => $va){ /////////////
//echo "<pre>";
//print_r ($va);
$ke;
$va;
//echo "<pre>";
print_r ($va);
}
$count = count($va);
echo "<br>";
echo $count;
}
echo "<pre>";
//print_r ($k);
//echo "<pre>";
//print_r ($v);
$k = $k;
$v = implode('',$v);
//echo $v;
$con = mysqli_connect('localhost','root','','loop');
$sql = "INSERT INTO loopt (`eid`,`date`,`name` ) VALUES ($v)";
$exe = mysqli_query($con,$sql);
?>
it's not a best way to do this, but still, you can try following
$data = array('eid' => array(124,658,457,145),
'data' => array('2012/10/12','2012/10/15','2012/10/22','2012/10/02'),
'name' => array('Chiku','Lipu','Babu','Dipu')
);
if (isset($data['eid']) && is_array($data['eid']) && sizeof($data['eid']) > 0)
{
$con = mysqli_connect('localhost','root','','loop');
foreach ($data['eid'] as $key => $value)
{
if (isset($data['data']) && isset($data['data'][$key]) && isset($data['name']) && isset($data['name'][$key]) )
{
$sql = "INSERT INTO loopt (`eid`,`date`,`name` ) VALUES ($value,$data['data'][$key],$data['name'][$key])";
$exe = mysqli_query($con,$sql);
}
}
}
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$index]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}
how to pass $index to array value? I am trying to check value of the index is greater than or not in if condition. How to get this?
With your code now it couldn't work since your print_r($val) result was :
Array
(
[S0001] => 2
[S0002] => 1
)
The key of your $vals are the result of your $suppliername array, so try like this maybe :
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$suppliername[$index]]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
}
}
The result :
S0001+++Multiple Entries
S0002+++Single Entry
S0001+++Multiple Entries
Is this what you are looking for?
I'm not sure i understood well what you wanted, but here it does what you want as result :
<?php
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
echo "<pre>";
var_dump($suppliername);
echo "</pre>";
echo "<pre>";
var_dump($vals);
echo "</pre>";
foreach($Product as $index => $value)
{
if($index>1)
{
echo "<br />" . $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo "<br />" . $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('ques', 'options')));
$query->from($db->quoteName('logical_ques'));
$db->setQuery($query);
$result = $db->loadRowList();
print_r($result);
foreach($result as $key => $value)
{
echo " ". $value;
}
?>
Hello everyone, I'm creating a website in Joomla and I want to display some values from the database into the article but not in array form and for this I've used foreach but it's giving me Array to string conversion error
$value seems to be an array. Please check using var_dump. Echoing an array gives this error.
The array_values seems to be array. So create a recursive function to return string from your array. The code should be something like this
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('ques', 'options')));
$query->from($db->quoteName('logical_ques'));
$db->setQuery($query);
$result = $db->loadRowList();
print_r($result);
if (is_array($result)) {
echo arrayToString($result);
} else {
echo $result;
}
function arrayToString($array)
{
foreach($array as $key => $value)
{
if (is_array($value))
{
$res .= arrayToString($value);
} else {
$res .= " ". $value;
}
}
return $res;
}
?>
I am getting the result from web service SOAP client. I have created an array to get the result and display it using json format. I am getting few of my results properly. I have SerialEquipment parameter which is array and i need to get the result using foreach loop. I am doing an mistake there. I dont know how can i assign my $vehiclResult array in this for each statement. So that all the results at last i will collect and display using json using vehicleResult array.My mistake is in the foreach loop.
structure for SerialEquipment parameters:
Code:
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$vehiclResult = array(
'WE_Number' => $vehicle['WE Number'] ."<br>",
'Vehicle Type'=> $vehicle['Vehicle Type'] . "<br>",
'HSN' => $vehicle['HSN'] . "<br>",
'TSN' => $vehicle['TSN'] . "<br>"
);
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['SerialEquipment'] = $key. "<br>";
$vehiclResult[$key]['Code'] = $obj->Code. "<br>";
$vehiclResult[$key]['Desc Short'] = $obj->Desc_Short. "<br>";
$vehiclResult[$key]['Desc Long'] = $obj->Desc_Long. "<br>";
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc). "<br>";
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc). "<br>";
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
}
You need to check if your array have the key so:
if(!isset($vehiclResult[$key]))
if not, you need to create it:
$vehiclResult[$key] = array(); // as an array
Also, you don't really need to make a description of your "item". You can Parse your JSON on the result page to output some text.
You can do something like.
Do something like:
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['serial'] = $key;
$vehiclResult[$key]['code'] = $obj->Code;
$vehiclResult[$key]['short_desc'] = $obj->Desc_Short;
$vehiclResult[$key]['long_desc'] = $obj->Desc_Long;
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2;
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode;
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc;
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode;
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc);
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode;
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc);
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
If you would keep your "text" and your <br> code, do the samething but add what you want to output after the "="
EDIT
** A HAVE CHANGE THE CODE PREVIOUSLY..
if you want to test your $vehiclResult, try something like:
foreach($vehiclResult as $key=>$value){
if(!is_array($value))
var_dump($value);
else {
foreach($value as $key2=>$value2){
if(!is_array($value2))
var_dump($value2);
else {
foreach($value2 as $key3=>$value3){
var_dump($value3);
}
}
}
}
$query2 = "SELECT * FROM `Listing` WHERE listingid = '{$myID}'";
if(!($result2 = # mysql_query($query2,$connection)))
echo "query failed<br>";
$result_array = array();
while($row2 = mysql_fetch_assoc($result2))
{
$result_array[] = $row2;
}
foreach ($result_array as $key => $val) {
echo "$key = $val\n";
}
Listing (Table) has 7 fields.
I get a bunch of "0 = Array"
How do I store mysql query results into php array and display them?
I want to have it as array first so i can sort them.
You are actually storing them. The code you have listed is storing them in array of arrays to see them you can modify the for with:
foreach ($result_array as $key => $val) {
echo "$key = " . print_r($val, true) . "\n";
}
$val is actually an array containing all the fields of a row.