How can I store results from MySQL to PHP array? - php

I tried storing data to php array from mysql using under code. But it's not working.
This code:
echo $answerArray[count][i];
shows the correct result. But this code:
echo $answerArray[0][0];
doesn´t show anything.
What should I do to fix it?
Thank you.
Full code:
$count = 0; //answer count
$answerArray = array();
while ($row = mysqli_fetch_array($resultFromR, MYSQLI_ASSOC)) { //add array from db
for($i = 0; $i < $questionNumber; $i++) {
$j = $i + 1;
$answerArray[count][i] = $row["num$j"];
echo $answerArray[count][i]; //is working.
}
$count++;
}
echo '<br />';
echo $answerArray[0][0]; //something wrong!!! I cannot get anything from this.

maybe this will work
$answerArray[$count][$i] = $row["num$j"];
(add $ before count and i)

Not sure but shouldn't the [i] be [$i]?

Related

Return json from function and pass to variable

I don't get it how to return a json from a function. Here's the code I tried:
function buildJson(){
... $json_source is parsed by http (this works)
$source = json_decode($json_source, true);
$res = $source['child'];
echo count($res); // 6
return $res;
//return json_encode($res);
}
//////
$json_res = buildJson();
echo count($json_res); // 0
for($i = 0; $i < count($json_res); $i++){
$item = $json_res[$i];
}
How does it work, I tried so many things but count for $json_res is 0 everytime? Sorry I don't have that much experience :)
EDIT: "$json_source" is generated inside the function buildJson(). I tested that it exists with "echo count($res); // 6". And I can access all the values of $res inside the function, but I can't return and access it outside. So "$json_source" is not the problem. Any other ideas?
SOLVED: Ok now I found the problem, for coding reasons I was calling buildJson() another time inside buildJson(). Now I coded it in another way and it works now. Thanks #all :)
Try to pass parameter to your function.
I think its not working because you don't have a data source for your json
function buildJson($json_source){
$source = json_decode($json_source, true);
$res = $source['child'];
echo count($res); // 6
return $res;
//return json_encode($res);
}
$json_res = buildJson($json_source);
echo count($json_res); // 0
for($i = 0; $i < count($json_res); $i++){
$item = $json_res[$i];
}
Try this Code.
$json_res = buildJson();
$json_res = json_decode($json_res,true);
echo count($json_res);
for($i = 0; $i < count($json_res); $i++){
$item = $json_res[$i];
}

Can you use a for loop to create array names?

I am pulling data from a database and I want to create array names on the fly.....group1, group2...etc using a for loop. I wonder if this is possible at all? The code below obviously doesn't work and I'm only including it to demonstrate what I'm trying to do. Any help would be much appreciated!
<?php
for ($i=1; $i <=40; $i++){
$group.$i = [];
}
?>
Its not so much "array names" that you are looking for as a much as a nested array. $group[$i] would give you a nested array. eg
for ($i=1; $i <=40; $i++){
$group[$i] = [];
}
$group[1][] = 'foo';
echo $group[1][0];
// prints: foo
Something like this ? This works 100%
// $result = $conn->query($sql);
$i = 0;
$all = array();
while($row = $result->fetch_assoc()) {
$i++;
$arr[$i]['id'] = $row['id'];
$arr[$i]['firstname'] = $row['first_name'];
$arr[$i]['lastname'] = $row['last_name'];
$all[] = $arr[$i];
}
echo '<pre>';
echo print_r($all);

Store images in array PHP

I'm really new at php just doing some work, I want to save images in a php array and then show them in the screen, but I cannot save them or display them.
<?php
$min = 1;
$max = 9;
$number1 = rand($min,$max);
for ($i=1 ; $i<=$number1 ; $i++){
$firstN [$i] = echo "<img src='index.jpg' border='0'>";
}
echo $firstN [1];
?>
This is what I got , and the last line is to test it but nothing works, I google the topic but it doesn't help.
Thanks in advance.
As long as index.jpg is in the same directory as your file, this should work:
<?php
$firstN = array();
$min = 1;
$max = 9;
$number1 = rand($min, $max);
for ($i = 0; $i < $number1; $i++){
$firstN[] = '<img src="index.jpg" border="0">';
}
echo $firstN[0];
?>
Cleaned up the code a bit. When storing information in the array, you don't use echo and, like Mister pointed out, you had a space in the echo at the bottom of the code between the array-variable and the brackets.

PHP + MySQL + CodeIgniter: Check if current row exists

I would like to optimize the script a little and save cpu some work, so I have to create for loop. In this loop I will work with some data from database and below is my current code:
$result = $this->Model->function();
for ($i = 0; $i < $result->num_rows(); $i++)
{
echo $result->row_array($i)['row'];
}
What do I need here is to check if the next row exists. This one would be on the top of all code in for loop in if statement, but there is some problem. If I type $result->num_rows()+1 in for loop and echo the last row (which doesn't exist) out, the value of it isn't null, but it's same as row before.
How do I check if current row is null?
Or rather than have the boolean checked every time, plus you don't call the num_rows method a second time:
$result = $this->Model->function();
$y = $result->num_rows();
for ($i = 0; $i < $y-1; $i++)
{
echo $result->row_array($i)['row'];
}
echo 'last row';
echo $result->row_array($y-1)['row'];
You can do this for example:
$result = $this->Model->function();
$y = $result->num_rows();
$y--;
for ($i = 0; $i < $result->num_rows(); $i++)
{
if ($i == $y)(
echo 'last row';
)
echo $result->row_array($i)['row'];
}

how do i reverse the order when echoing my results?

i have this bit of code:
<?php
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?>
and currently it echos the links in order they appear on the website. I would like for it to echo the results in a reverse order (the last link on example.com to echo as the first with this code)
any help is appreciated! thanks.
for ($row = $count - 1; $row > -1 ; $row--) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
PHP has a function for it: array_reverse.
$a[1] = array_reverse($a[1]);
Also why would you use ["$row"] instead of [$row]? There is no functional difference as numeric strings get converted back to numbers when using them as indexes, so don't worry about that, but just because something can be done doesn't mean you should do it.
<?php
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$a = array_reverse($a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?>

Categories