Get data from mysql as array [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sorry for my english..
in mysql has row name iurl.. There is data: 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg
i get its as:
<?php $s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
if($s){
$array = array();
while($t=mysql_fetch_array($s)) {
$array[] = $t['iurl'];
}
print_r($array);
?>
it gives me result: Array ( [0] => 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg )
And i`m need get it and print like a link
how can i do it?
Thanks..

You can use explode(); to split the string into an array and then loop over to print each item:
$images = explode(",", $t["iurl"]);
foreach ($images as $image) {
echo "{$image}";
}

I think you are asking to have result or in your case a .jpg file be the href of your link? If so do this:
......

The result you get from your database as you already figured out is an Array
The only thing you need to do is loop trough the array.
ps: consider using mysqli more info at php.net http://www.php.net/manual/en/book.mysqli.php
<?php
$s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
while ($row = mysql_fetch_object($s)) {
echo '<img="http://yourhost.com/images/'.$row->image.' alt=""/>';
}
?>

Related

PHP multidimensional array loop through second dimension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how do i loop this kind of array?
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
I want to loop through the column of each item.
e.g.: i want to display ('apple','373','t0','1') at the first loop and ('orange','22','h0','0') at the last loop. thanks
We are assuming that all the arrays inside the main array are the same size in this example.
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
for($i = 0; $i<sizeof($arr["aa"]); $i++)
{
foreach($arr as $key=>$item)
{
echo($item[$i]);
}
echo ' - ';
}
Output: (obviously you can do any necessary formatting you need such as new lines or commas)
apple373t01 - orange22h00 -

How to return array unique value? in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need an array like this in PHP?
array = [mani , mani, nithi, nithi, basto]
return in
array = [basto]
not for other elements
Does anyone know how to solve this problem?
<?php
$items = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$counts = array_count_values($items);
// Remove elements that occur more than once.
$filtered = array_filter($items, function ($item) use ($counts) {
return $counts[$item] === 1;
});
var_export($filtered);
Output:
array (
4 => 'basto',
)
You may want to specify your question so users here can provide better assistance.
To your issue:
array_unique($array);
https://www.php.net/manual/en/function.array-unique.php
EDIT: you want to search all items by name, to do that you need this function: https://www.php.net/manual/en/function.array-search.php
with your example:
$array = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$basto = array_search('basto', $array);
Best,
Sebo

How to use foreach array value out side of loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo $value; // Result: 14
the result inside loop is: 10,11,12,13,14
and outside loop is : 14
I want to use all value outside of loop
Use below code to get your array values comma seperated
implode(",", $reciveValue)
Try this :
$value = '';
foreach($reciveValue as $val){
$value .= $val.",";// Result: based on user input like:10,11,12,13,14
}
echo rtrim($value, ',');
#Support Techcherry if you want to use all values of an array outside
the loop then you can use implode() function, below is an example
understand
<?php
$reciveValue = array(10,11,12,13,14); // suppose this is your array
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo "<br>";
echo implode(',', $reciveValue); // here implode() function convert the array value in the string form
?>
try it, it will help you
thanks to everyone for posting answer
every answer is very helpful for me
but finally i got my solution
$result="";
foreach($reciveValue as $value)
{
$result=$value.",".$result;
}
echo $result;

display each item in the array without using a loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i have this certain php function. But i cannot figured it out how to output this one. This is the question // Given an array, display each item in the array without using a loop. (Do not use built in functions to do this, like PHP's print_r; use a recursive function to implement)
and this is the code
<?php
function print_array(array $input)
{
}
?>
can someone share ideas on this? Any help is muchly appreciated.
you can use implode
$your_array = array("abc", "5", "xyz")
$text = implode(" ", $your_array); // implode with space you can use any other on this place
echo $text;
OUTPUT : abc 5 xyz
FUNCTION :
function print_array($your_array)
{
$text = implode(" ", $your_array); // implode with space you can use any other on this place
echo $text;
}
Recusive Method :
but only used when the key of the array is numeric starting with 0, 1, 2 ....
print_array($your_array);
function print_array($your_array, $index=0)
{
if(isset($your_array[$index]))
{
echo $your_array[$index];
$index+=1;
print_array($your_array, $index)
}
}

PHP how to add values to array by function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want do add values to array in my function to show movies:
$movies = array();
function find_movie($tag) {
// SELECT MOVIE BY %TAG%
$movies[] = $MOVIE;
return 'WATCH MOVIE';
}
Induction:
echo find_movie('cars'); // WATCH MOVIE
My problem is here -> $movies array is empty...
I want to have array with all movies what is showed.
How can I do that ?
You need to pass the $movies array as a reference to make this work out.
Like this..
function find_movie($tag,&$movies){
and call it like..
echo find_movie('cars',$movies);
The code..
<?php
$movies = array();
function find_movie($tag,&$movies) {
$movies[] = "<a href=$tag>WATCH MOVIE</a>";
}
find_movie('cars',$movies);
var_dump($movies);
OUTPUT:
array (size=1)
0 => string '<a href=cars>WATCH MOVIE</a>' (length=28)

Categories