Get two results while looping trough associative array in PHP - php

I want to get two results at a time when using while looping trough a associative array in PHP.
I need to echo two results per row, something like this:
<?
while($row = mysqli_fetch_assoc($result)) {
$client_name = $row['client_name'];
$review = $row['review'];
echo('
<div class="row">
<div>
<p>'.$client_name.'</p>
<p >'.$review.'</p>
</div>
<div>
<p>'.$client_name.'</p>
<p >'.$review.'</p>
</div>
</div>
');
}
}
?>
Right now it's giving me the same result twice rather than the next one.

You can use a counter to control the output of the outside div so that you get two inside div output for each outside one:
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
$client_name = $row['client_name'];
$review = $row['review'];
if ($i % 2 == 0) echo '<div class="row">';
echo '<div><p>'.$client_name.'</p><p>'.$review.'</p></div>';
if ($i % 2 == 1) echo '</div>';
$i++;
}

Related

Display row in database with same data in particular div

Lets say I have data in my database with some rows having the same data.
id|value|message
1|001. |one
2|001. |five
3|002. |four
4|001. |hello
5|001. |sup
6|002. |sure?
Is it possible i echo all message data with 001 together in one <div class="display"></div> and those with 002 with the same <div class="display></div> There by having two <div class="display"></div> display the count of the two different values.
I have tried with PHP using
$sql =<<<EOF
SELECT value, COUNT(value) AS NumOccurrences FROM table WHERE id != '$log_id' GROUP BY value, message ORDER BY id DESC;
EOF;
$ret = $db->query($sql);
while ($row = $ret->fetchArray(SQLITE3_ASSOC))
{
$value = $row['value'];
echo "<div class='display'>$value</div>";
}
But the above code echo six <div class="display"></div>. That is each div for each value
<div class="display">001</div>
<div class="display">001</div>
<div class="display">002</div>
<div class="display">001</div>
<div class="display">001</div>
<div class="display">002</div>
But i want something like this
<div class="display">001001001001</div>
<div class="display">002002</div>
Please I am new to PHP and searched hard for an answer. Is this possible?
Without changing query you can do something like that:
$by_val = array();
while ($row = $ret->fetchArray(SQLITE3_ASSOC))
{
$value = $row['value'];
if(!isSet($by_val[$value])) $by_val[$value] = '';
$by_val[$value] .= $value;
}
foreach($by_val as $key => $str) {
echo "<div class='display'>".$str."</div>";
}

how can we handle while loop iteration?

I am having block of code which is working fine but the problem is that...
in one loop it print 'one' qoute.
in 2nd both 'one' and 'two'
in third all three
...so on till the loop execute.
I want to print only one in first time and second qoute in second time and so on till loop executes.
here is my block of code
<div class="records round">
<?php
//show records
$query = mysqli_query($con,"SELECT table2.col2 AS a,table1.col2 AS b, table1.col1 AS c, table1.q_url AS d
FROM {$statement}
LIMIT {$startpoint} , {$limit}");
$output='';
$Authorname='';
$count=1;
while ($row = mysqli_fetch_array($query)) {
$Authorname =$row['a'];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url=explode('/',$url);
?>
<div class="record round"><?php echo $count; $output .='<a href="http://localhost/quotes/'.$url[5].'/'.$row['d'].'.html">';
echo $output .=$row['b'].'</a>';?></div>
<?php
$count++;
}
?>
</div>
What you are doing here appending string to $output variable so at each iteration its keeping appending.
Your $output variable must be empty before performing new iteration.
Thats the reason you are getting such output.
So to avoid this you have to reinitialize $output variable inside the loop.
Check below code:
while ($row = mysqli_fetch_array($query)) {
$output='';
$Authorname =$row['a'];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url=explode('/',$url);
?>
<div class="record round"><?php echo $count; $output .='<a href="http://localhost/quotes/'.$url[5].'/'.$row['d'].'.html">';
echo $output .=$row['b'].'</a>';?></div>
<?php
$count++;
}

Creating 'Browse user' page

I have a table for users already created so for example I will run a query which will SELECT * FROM users WHERE accType='1' and I am looking to run a foreach loop on the results and put each result into this element
<div class="title-desc-wrap col-xs-12 col-md-8">
<div class="title-wrap "><h4>User Name</h4></div>
<div class="excerpt-wrap">PROFILE PICTURE</div>
</div>
I want to restrict the page to only show 8 users on each page. How would I go about structuring this foreach loop in PHP?
Not too sure on foreach but for a for loop, if you are placing the results into an array, you could;
<?php
for($count = 0; $count < 8; $count++) {
$holder = $array_name['$count'];
echo "<a href=LINK TO USERS PROFILE>". $holder['userName'] ."</a>";
echo "<div class=excerpt-wrap>". $holder['ProfilePic'] ."</div>";
}
then for the next page;
<?php
for($count = 8; $count < 16; $count++) {
$holder = $array_name['$count'];
echo "<a href=LINK TO USERS PROFILE>". $holder['userName'] ."</a>";
echo "<div class=excerpt-wrap>". $holder['ProfilePic'] ."</div>";
}
and so on, bit basic but it should work.

MYSQLI fetch_array not working

I am trying to figure out why my mysqli query is not returning all the rows. For some reason it returns 3 results when there are 4 in the database. It is completely skipping the first record in the database. Here is my query.
$results = "SELECT * FROM `results` LIMIT 10";
$result = $conn->query($results);
if ($result) {
?>
<div id="tableResults">
<div class="row1 bg">Predicted Sex</div>
<div class="row2 bg">Suggested Baby Boy Name</div>
<div class="row3 bg">Suggested Baby Girl Name</div>
<div class="breaker"></div>
<?php
/* fetch object array */
$i = 0;
$count = count($result->fetch_array());
while ($row = $result->fetch_array()) {
?>
<div class="row1 <?php if (!$i == $count - 1) { echo 'customborder'; } ?>"><?php echo $row['sex']; ?></div>
<div class="row2 <?php if (!$i == $count - 1) { echo 'customborder'; } ?>"><?php echo $row['boy_name']; ?></div>
<div class="row3 <?php if (!$i == $count - 1) { echo 'customborder'; } ?>"><?php echo $row['girl_name']; ?></div>
<?php
$i++;
}
}
$conn->close();
?>
As was stated in the comments $count = count($result->fetch_array()); this will not work as expected and makes you lose one row (as it has been fetched). Instead you can use num_rows like the following
$count = $result->num_rows;
while ($row= $result->fetch_array()) {
//...
}
To go into detail, when you read the manual on fetch_array(), you'll find this part
mysqli_result::fetch_array -- mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both
A result row (in words: one) will be fetched any time this function is called. So currently your code is similar to:
fetch one row -> do nothing with it
while:
fetch one row -> display it

Assigning new array from existing array php only shows last value of array

I have a slight problem here. I have the following code that I create a search query, and im trying to take "$row" array and create a new array called "$item" so I can echo the values inside my div statement. This code works, and I do echo the $item array with the 'follower_username' values, however only the last value gets echoed out. I have values inside my database Nathan, Brett, Nathan2 and it only echoes Nathan2, or the last value of the array $item.
Here is my code:
$result = mysqli_query($con,"SELECT * FROM followers
WHERE username='$username'");
?>
<?php
while ($row = mysqli_fetch_array($result))
{
$item = $row;
}
?>
<body>
<div id="contentwrap">
<div rel="scrollcontent2">
<?php echo $item['follower_username'];?> <img style="width: 50px; height: auto;"<?php echo '<img src="/user_photo/' . $item['follower_username'] . '.jpeg" />';?>
</div>
I hope I can get this resolved, it is bugging me! Thank you for any help!!
What you're doing is reassigning the value of item to the current row each time.
$row will be an object of table row values from the resulting query so if you want all the rows in an $item variable that would need to be an array to store each result, instead of storing the current row in the same variable for each row.
You will then need to iterate that array to display each result.
$i = 0;
$item = array();
while ($row = mysqli_fetch_array($result))
{
$item[$i] = $row;
$i++;
}
<div>
<?php
foreach ($item as $value)
{
echo $value['follower_username'];
}
?>
</div>
Below is an example to show some shorthand versions of the same idea:
$item = array();
while ($row = mysqli_fetch_array($result))
{
$item[] = $row;
}
<div>
<ul>
<?php foreach ($item as $value): ?>
<li><?php echo $value['follower_username']; ?></li>
<?php endforeach; ?>
</ul>
</div>
while ($row = mysqli_fetch_array($result))
What that does is grab the next row in the result set, and assign it to the variable $row. Each time the loop runs, it assigns the latest row to $item. So to store the whole result set, use
$item[] = $row.
I'm sure what you are trying to accomplish with our next line of code. To show all items you need something like
foreach($item as $singleItem)
{
echo $singleItem['follower_username']."<br>";
}

Categories