How can I use one by one sql data in php? - php

I'm using PDO::FETCH_ASSOC and i'm trying to get data in mysql. This is my sql data:
Array
(
[lessons_media_id] => 1
[lessons_id] => 1
[lessons_media_photo] => http://link1
)
Array
(
[lessons_media_id] => 2
[lessons_id] => 1
[lessons_media_photo] => http://link2
)
I use foreach:
<?php
foreach($data as $key => $value){
echo $value["lessons_media_photo"];
}
?>
Outputs:
http://link1 http://link2
I wanna use one by one. How can I do this ?
Examples:
<p>http://link1</p>
<img src="http://link2">

Try This:
<?php
foreach($data as $key => $value){
echo '<p>'.$value["lessons_media_photo"].'</p>';
echo '<img src = '.$value["lessons_media_photo"].'>';
}
?>

Related

Change value in multidimension associative array PHP

I must be missing something about how PHP arrays are handled. When I execute the following code:
<?php
$ary = array(
"alpha" => array("A"=>1,"B"=>2,"C"=>3),
"beta" => array("A"=>7,"B"=>8,"C"=>9)
);
foreach ($ary as $key => $vals) {
$vals["B"]=99;
echo $key."= ".$vals["A"]." ".$vals["B"]." ".$vals["C"]."<br>";
}
echo $ary['alpha']["B"]."<br>";
?>
I get:
alpha= 1 99 3
beta= 7 99 9
2
The change to 99 in each case seems to be lost. What am I doing wrong?
If you want to change items of array in foreach statement you should pass by reference.
foreach ($ary as $key => &$vals) {
}
<?php
$ary = array(
"alpha" => array("A"=>1,"B"=>2,"C"=>3),
"beta" => array("A"=>7,"B"=>8,"C"=>9)
);
foreach ($ary as $key => $vals) {
//$vals["B"]= 99;
$ary[$key]["B"] = 99;
echo $key."= ".$vals["A"]." ".$vals["B"]." ".$vals["C"]."<br>";
}
echo $ary['alpha']["B"]."<br>";
?>

PHP Arrays Values

Please provide help on how to write array values if I have array like this:
Array (
[937245328] => $0.79
[310776983] => $0.53
[720315389] => $0.39
[310800933] => $0.30
[1011934667] => $0.28
[1576813623] => $0.21
[926978479] => $0.19
[1011934570] => $0.14
[937244096] => $0.14
[310777321] => $0.13
[384801319] => $0.13
[519987816] => $0.12
[992123310] => $0.11
)
I would like to print this array out somelike this:
937245328: $0.79
310776983: $0.53
720315389: $0.39
and so on... Thanks for any help.
if your array is called
$myArray
Then you can print it out as follows:
<?php
foreach ($myArray as $key => $value)
{
echo $key.": ".$value."<br>";
}
?>
Let me know if that worked for you! :)
You can print the array (the key and the value) using foreach loop.
<?php
$dolla = Array (
'937245328' => '$0.79',
'310776983' => '$0.53',
'720315389' => '$0.39'
);
foreach($dolla as $key => $value) {
echo $key.": ".$value."<br>";
}
?>

reading multiple arrays in PHP

I need some help reading the values from multidimension arrays. The array looks like below.
Array
(
[translations] => Array
(
[0] => Array
(
[translatedText] => fantasma
[detectedSourceLanguage] => en
)
)
)
I tried the following, but kept on getting blanks. Any help be appreciated?
foreach($item as $translations)
{
foreach($row['0'] as $k)
{
echo $k['translatedText'];
echo $k['detectedSourceLanguage'];
}
}
When working with foreach loops, you want to call the array you plan on iterating over with the following syntax:
foreach($array as $variable){ }
Array being the array you plan on going through, and the variable being the variable you are planning to call it as within the foreach.
More information on foreach loops can be found at PHP:foreach
With that said, try the code below:
$data = array(
"translations" => array(
array("translatedText" => "fantasma",
"detectedSourceLanguage" => "en"
)
)
);
echo "<pre>";
echo print_r($data);
echo "</pre>";
foreach($data["translations"] as $translation) {
echo $translation['translatedText'] . "<br />";
echo $translation['detectedSourceLanguage'] . "<br />";
}
//Or, if the $data variable will be holding multiple translation arrays:
foreach($data as $d) {
foreach($d as $translation){
echo $translation['translatedText'];
echo $translation['detectedSourceLanguage'];
}
}
Try this:
foreach ($item['translations'] as $translation) {
echo $translation['translatedText'];
echo $translation['detectedSourceLanguage'];
}
See DEMO
Change your code to below :
$test = Array(
"translations" => Array (
"0" => Array (
"translatedText" => "fantasma",
"detectedSourceLanguage" => "en"
)
)
);
foreach ($test as $translations) {
foreach ($translations as $k) {
echo $k["translatedText"];
echo "<br/>";
echo $k["detectedSourceLanguage"];
}
}
This should work.
Follow this for more info about array : http://php.net/manual/en/language.types.array.php
The issue is that you are not defining the $row variable. The good news is that you don't need it.
You can simply do this:
foreach($item as $translations => $values)
{
foreach($values as $k)
{
echo $k['translatedText']."\n";
echo $k['detectedSourceLanguage'];
}
}

Trying to have an array with 2 values in side by side

I need to create an array where i can use 1,2,3 but have 1v1, 2v2, Clubs associated with them.
I am going to use 1,2,3 for option value and 1v1,2v2 and clubs to display to the user.
How can I store this in an array and then use foreach to extract?
Thanks
$data = array(
1 => '1v1',
2 => '2v2',
3 => 'Clubs';
);
echo '<select>';
foreach($data as $value => $title) {
echo '<option value="'.$value.'">'.$title.'</option>';
}
echo '</select>';
You can make your original values (1,2,3) the keys and the (1v1, 2v2, Clubs) the values.
$data = array(1 => '1v1', 2 => '2v2', 3 => 'Clubs');
foreach($data as $key => $value) {
print $key.' - '.$value.'<br/>';
}
Maybe, I don't understand your question. try to use the next array:
$array = array(1 => '1v1', 2 => '2v2');
And foreach:
<select>
<? foreach ($array as $k => $v) { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<? } ?>
</select>
I don't fully understand the question, but it seems like you want to map the values 1,2,3 to the text '1v1','2v2','Clubs'. PHP supports associative arrays that are suitable for this purpose:
$a = Array(
1 => '1v1',
2 => '2v2',
3 => 'Clubs'
);

PHP array keys - how to display?

I'm doing some work with a Google Analytics class. I get output as below:
Array
(
[20090401] => Array
(
[ga:pageviews] => 5000
[ga:visits] => 2500
)
[20090402] => Array
(
[ga:pageviews] => 5000
[ga:visits] => 2500
)
etc. How do I get the data to display in a table with the first column showing the date? ie the key for each array element.
like this:
20090401-----5000-----2500
Try this:
<?php
foreach ($report as $date=>$item) {
print($date.'-----'.$item['ga:pageviews'].'-----'.$item['ga:visits']);
}
?>
The piece you were missing was assigning the optional variable for the key in your foreach.
I'm not quite sure what you're asking but maybe this would help...
<?php
foreach($array as $key => $value)
{
echo $key . " => " . $value;
}
?>
Untested, but here's the idea...
foreach ( $report as $item => $data ) {
echo implode( '-----', array( $item, $data['ga:pageviews'], $data['ga:visits'] ) );
}
foreach($array AS $date => $data){
echo '
<tr>
<td>'.$date.'</td>
<td>'.$data['ga:pageviews'].'</td>
<td>'.$data['ga:visits'].'</td>
</tr>';
}
Check the php documentation about the foreach construct if you did not know about it.
The PHP function array_keys might help you out too: http://us.php.net/manual/en/function.array-keys.php

Categories