how does
while($result=mysqli_fetch_assoc($query)){ echo $result['name'];}
iterate all the rows in the table, i mean it should keep printing the first row only how does it print all rows i think i am fetching only the array of first row
Take an example of this loop
$i=0;
while($i<5)
{
// do something
$i++;
}
That loop will only stop when your condition ($i<5) results in FALSE
When you do
while($result=mysqli_fetch_assoc($query))
This loop willl also keep working as long as your fetch returns a row, which is not FALSE. The only different thing happening here is that you are not doing the increment yourself, when mysqli_fetch_assoc fetches a row it automatically increments the internal pointer of that result set to the next row so when you call the same function again you get the next row and when there are no more rows left it will return NULL which means $result=NULL; which will result in FALSE and you will exit the loop.
I have a database result set created from a PDO object.
The array contains all the data. Now I want to print the results but I want to start the printing from a specific index and not from the start.The start index is specified from a user. Please don't tell me to modify the query because this is not what I want. Also I've searched everywhere and didn't found any solutions to this.
I simplified my code so it's more understandable and easier to come to the point.
Thanks for any-kind of help. :>)
$res2=$conn->prepare("SELECT COUNT(*) FROM blogs");
$res2->execute();
while($r=$res2->fetch(PDO::FETCH_BOTH)){
// I have 37 records in $res2 and want to start echoing from record number 10.
//for example I want to echo out $r['title'] but not from the first but from the 5th or 10th index.
}
Use an if() statement and a counter:
$userInput = 5;
$i = 0;
while($r=$res2->fetch(PDO::FETCH_BOTH)){
if($i >= $userInput){
// echo your output here
}
$i++;
}
i've a array problem couldn't just solve it:
here's the code that is already in a foreach loop. i'm getting the $row->class_id value from this loop. it works fine and get me the total students row within an array and i preserved it in $total_student_of_this_class variable. i used another foreach loop for storing the result of first loop and then second loop and so on. but this loop gives only first loop result.
i need to combine the all array of total iteration of the loop.
$total_student_of_this_class =
$this->db->select('student_id')->where('class_id',
$row->class_id)->get('student')->result_array();
echo count($total_student_of_this_class);
// prints 2 in the first row of the output table and 5 in the second
row for me.
$total_student = array();
foreach ($total_student_of_this_class as $tt)
{
$total_student[] = $tt;
}
echo '<pre>';
print_r($total_student_of_this_class);
echo '</pre>'
echo count($total_student);
// prints only 2 outside the loop (not 7)
pls someone help me.
The problem here is seems to be with $total_student = array();: initializing array inside an other loop it always starts it from zero values array, so you get only results from one iteration inside it and doesn't collect all datas.
You can also look at array_merge php function.
I have a PHP array created by the mysqli_fetch_array() function. The array holds several rows, and without looping through them, can I grab each row one by one?
I have tried:
$links= mysqli_fetch_array($links_result);
echo $links['link'][0];
But I can't seem to get it to work. What could I be doing wrong? Is this possible?
when you do
$links= mysqli_fetch_array($links_result);
you get the next row in result set in $links just access them by the column names.
you can loop over the results like
if(mysqli_num_rows($links_result)) {
while($links= mysqli_fetch_array($links_result)) {
//each row in here
var_dump($links);
}
}
If the field name is 'link'
Without looping:
echo $links[0]['link'];
where '0' is your row number
so, for row #505 it will be:
echo $links[505]['link'];
Just make sure it exists ;)
Without loop you can grab each row of array using recursive function
like this:
function printarray_withoutloop($userarray,$elementindex)
{
if(count($userarray) > $elementindex + 1)
{
echo $userarray[$elementindex]['link'];
printarray_withoutloop($userarray,$elementindex + 1)
}
elseif(count($userarray) == $elementindex + 1)
{
echo $userarray[$elementindex]['link'];
}
}
$links= mysqli_fetch_array($links_result);
$elementindex=0;
printarray_withoutloop($links,$elementindex);
$countsara= array(array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"david"), array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"kaser"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"albert"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"stephen"));
foreach ($countsara as $tommykey=>$tommyval) {
foreach ($tommyval as $tommyvalkey=>$tommyvalval) {
echo $tommyvalkey.' - '.$tommyvalval;
echo '<br>';
echo 'test';
}
}
For example, there are 100 arrays. I want to get the first array values and insert into db, then 2nd and then until 100th array values.
I can able to insert the value into db but the issue is, if there are 100 arrays. The foreach is executing for 100 times. So the insert is happening for 100 times for a same id on a same record row. Which is a re-insert not a duplicate.
If there are totally 100 arrays in $tommyval. As usual, the test is being executed for 10 times. I am going to do an operation in the place of echo test; So I don't want to repeat the same operation 100 times.
How to avoid this in PHP foreach array repetition at the same time I want all the array to be executed?
$countsara= array(array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"david"), array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"kaser"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"albert"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"stephen"));
foreach ($countsara as $tommykey=>$tommyval) {
foreach ($tommyval as $tommyvalkey=>$tommyvalval) {
echo $tommyvalkey.' - '.$tommyvalval;
echo '<br>';
}
echo 'test';
}
The change that I made was placed the echo 'test'; after the completion of second foreach. I got what I wanted. I executed only once and so as the insertion into db also happens only once.
if you mean that you want to execute the insert portion of the code 10 times but //echo 'test'; portion should only be executed once, try setting a flag which will only let the //echo part once and stop it from executing with every loop