I had this code
echo "<pre>";
print_r($data);
echo "</pre>";
through which i got the following data
Spreadsheet_Excel_Reader Object
(
[sheets] => Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 2
[numCols] => 5
[cells] => Array
(
[1] => Array
(
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
)
)
)
)
)
I needed numCols so i used the following code
echo $data->sheets[0]['numCols'];
and got the result 5
Now i wish to fetch the following values
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
and store them in the table like this
id value
1 sno
2 candidta name
3 email
4 ctc
5 location
Can anyone please tell how it can be done
$Data = array();
foreach($data->sheets[0]['cells']['1'] as $key=>$value):
$Data[] = "('".$key."','".$value."')";
endforeach;
mysql_query("INSERT INTO tbl_name(`id`,`value`) VALUES".implode(",",$Data)) or die (mysql_error());
$newarray = $data->sheets[0]['cells'][1];
for ( $i=1; $i < count($newarray); $i++)
{
echo $newarray[$i];
$sql= "INSERT INTO `tablename` (value) VALUES ('$newarray[$i]')";
if(!mysqli_query($con,$sql))
{
echo "Error: " . $sql. "<br>" . mysqli_error($con);
}
}
Related
how can i get the first row of result? Below is my code which is giving me an error like this Undefined index: module
if ( substr( $action, 0, 4 ) === "stl_" )
{
$query = "SELECT * FROM a_actions LEFT JOIN a_modules ON ( a_modules.id=a_actions.module_id )
WHERE a_actions.id=(SELECT dependency FROM a_actions WHERE action='{$action}') AND a_modules.module_status = 1 ";
$action = \Yii::$app->db->createCommand( $query )
->queryAll();
//print_r($action);die();
$module = $action[ 'module' ];
$action = $action[ 'action' ];
}
$action has value
Array ( [0] => Array ( [id] => 7 [module_id] => 7 [action] => index [label] => Members [dependency] => [created_by] => [created_at] => [updated_by] => [updated_at] => [is_deleted] => 0 [module] => members [module_name] => [module_status] => 1 ) )
in Yii1 i would have used
$action = \Yii::$app->db->createCommand( $query )
->queryRow();
You can use queryOne()
\Yii::$app->db->createCommand( $query )
->queryOne();
QueryOne()
My code gave this warning, but when i print_r the row, it display the value selected.
So how do i solve this
This is the code "
$query3 = ("Select b.*, c.ScrubNurseName from scrubnurse b, surgery a, scrubnurselist c
where b.rn_no = '$id'
and a.SurgId = b.SurgId
and b.ScrubNurse = c.ScrubNurseID
ORDER BY b.SurgId, b.SerialNo");
$result3 = mysql_query($query3) or die(mysql_error());
$row3 = mysql_fetch_array($result3);
<?php
/*try utk display scrubnurse */
print_r($row3);
while($row3 = mysql_fetch_array($query3)){?>
<p>Scrub Nurse : <?php echo $row3['ScrubNurse'];?>
<p>Status : <?php echo $row3['Status'];?>
<?php }
and this is the result from print_r($row3)
Scrub Nurse Array ( [0] => 16
[SerialNo] => 16 [1] =>
rand52fad80207b6a1.33040579 [SurgID] => rand52fad80207b6a1.33040579 [2]
=> RN001-13 [Rn_No] => RN001-13 [3] =>
2014-02-10 [Surg_Date] => 2014-02-10 [4] => 015405 [ScrubNurse] =>
015405 [5] => C [SNRole] => C [6] => Azhari Landut [ScrubNurseName] => Azhari
Landut )
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\cotds3\editpt_surgery2.php on line 470
In line while($row3 = mysql_fetch_array($query3)){? there is typo it should be while($row3 = mysql_fetch_array($result3)){?
Now I have 3 row of data.
id name date
1 data1 12-06-2013
2 data2 12-06-2013
3 data3 16-06-2013
now I want to group the 3 rows of data into a new array, something like:
Array
(
[2013-06-12] => Array
(
[0] => data1
[1] => data2
)
[2013-06-16] => Array
(
[0] => data3
)
)
the question is 1st i using foreach to show all date out.
<?php foreach($datax as $data):
echo $data['Model_Name']['id'];
endforeach;?>
then, while i looping i want to group up the $data sort to date as the multidimensional array as above.
Try below method,. you can add an additional column for date
$a = array(
array('datax' => array('id' => 1, 'name' => 'data1')),
array('datax' => array('id' => 2, 'name' => 'data2')),
array('datax' => array('id' => 3, 'name' => 'data3'))
);
$result = Set::classicExtract($a, '{n}.datax.id');
/* $result now looks like:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
$result = Set::classicExtract($a, '{n}.datax.name');
/* $result now looks like:
Array
(
[0] => data1
[1] => data2
[2] => data3
)
*/
I'm trying to get specific value by array name:
<?php
$json = json_decode($_POST['json'], true);
print_r($json);
?>
I'm getting this varray:
Array
(
[0] => Array
(
[name] => pav
[value] => g
)
[1] => Array
(
[name] => ppav
[value] => f
)
[2] => Array
(
[name] => kiekis
[value] => g
)
[3] => Array
(
[name] => kaina
[value] => g
)
[4] => Array
(
[name] => ppav
[value] => f
)
[5] => Array
(
[name] => kiekis
[value] => g
)
[6] => Array
(
[name] => kaina
[value] => f
)
[7] => Array
(
[name] => ppav
[value] => g
)
)
Tried using foreach function, but cant get specific value:
foreach ($json as $key => $value) {
echo "name".$key['name']." value".$value['value']."<br />";
}
It prints all array values:
name value<br />name valueasd<br />name valueasd<br />name values<br />name values<br />name values<br />name values<br />name valuea<br />name valueasd<br />name valued<br />
But I cant select specific value by name to add to nysql. How to do that?
Following is the tested code
<?php
$json_array = array(
array('name'=>'pav', 'value'=>'g'),
array('name'=>'ppav', 'value'=>'f'),
array('name'=>'kiekis', 'value'=>'g'),
array('name'=>'ppav', 'value'=>'f')
);
echo "<pre>";
print_r($json_array);
echo "</pre>";
$assoc_array = array();
for($i = 0; $i < sizeof($json_array); $i++)
{
$key = $json_array[$i]['name'];
$assoc_array[$key] = $json_array[$i]['value'];
}
echo "<pre>";
print_r($assoc_array);
echo "</pre>";
echo "assoc_array['pav'] = ".$assoc_array['pav'];
?>
output of the code is given below and you can see that exactly same array as yours is converted into associative array, there is one problem as your array has repeated names eg. ppav or kiekis so there will only 1 index for kiekis or ppav having the latest value.
you have to recreate array
$json_array = json_decode($_POST['json'], true);
$assoc_array = array();
for($i = 0; $i < sizeof($json_array); $i++)
{
$key = $json_array[$i]['name'];
$assoc_array[$key] = $json_array[$i]['value'];
}
after this you will get $assoc_array and you can access its elements by keys.
I have the following code:
<?php
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
echo "<pre>"; print_r($details); echo "</pre>";
?>
The result is:
Array
(
[destination_addresses] => Array
(
[0] => 150th St, Summit, SD 57266, USA
)
[origin_addresses] => Array
(
[0] => 38591 Patterson Rd, Wanette, OK 74878, USA
)
[rows] => Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 1,299 km
[value] => 1299343
)
[duration] => Array
(
[text] => 13 hours 26 mins
[value] => 48361
)
[status] => OK
)
)
)
)
[status] => OK
)
I need to extract from $json only the distance (1,299 km).
I am not sure how to do this.
Any help is very much appreciated.
<?php $q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json);
$distance=$details->rows[0]->elements[0]->distance->text;
echo $distance; ?>