Im wondering what can I do to make images from a database on Php display on my page.
This is what I have
images .php
$query = "SELECT * FROM images ORDER BY name ASC ";
$result = $db->query($query);
$num_result = $result->num_rows;
echo "<h1> Images</h1>";
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array = array($URL);
}
foreach ($array as $image){
echo '<tr>';
echo '<td><img class="coupons" src="'.$image.'"/></td>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
}
This is just printing only one image and I have 10 in my database, what can I do or change to print all of the images from the database? Thanks
You should change
$array = array($URL);
into
$array[] = $URL;
And add before line:
for ($i = 0; $i < $num_result; $i++){
add line:
$array = array();
Try this,
$array = array();//initialize here
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array[] = $URL;
}
You can rewrite your code as,
while ($row = $result->fetch_assoc()){
$name = $row['name'];
$URL = $row['imageURL'];
echo '<tr>';
echo '<td><img class="coupons" src="'.$URL.'"/></td>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
}
$query = "SELECT * FROM images ORDER BY name ASC ";
$result = $db->query($query);
$num_result = $result->num_rows;
$array = array();
echo "<h1> Images</h1>";
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array[] = URL;
}
foreach ($array as $image){
echo '<tr>';
echo '<td><img class="coupons" src="'.$image.'"/></td>';
echo '<td></td>';
echo '</tr>';
}
You also had a trailing <tr> which may cause styling issues
Related
I have the following code where I try to print alphabets from a to z in the first column. but I only get the letter "a" is printed through the entire column.
function print_table_tb ($conn, $id) {
$sql = "SELECT stata,statb,statc FROM dbA";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='tbr' id='tb$id'>";
while($row = $result->fetch_assoc()) {
$data = array_reduce($row, function($carry, $value) {
$carry[] = "<td dbval='{$value}'>{$value}</td>";
return $carry;
}, []);
$range = range('a', 'z');
$i = 0;
echo '<tr><td class="tbe">'.$range[$i++ % 26].'</td>'. implode('', $data) . "</tr>\n" ;
}
echo "</table>";
}
}
What am I doing wrong here? thanks for your help! My question is different. I don't seek a solution to get alphabetical listing, I have the solution, but I couldn't fix an error.
I tried this but it doesn't work either.
function print_table_tb ($conn, $id) {
$sql = "SELECT stata,statb,statc FROM dbA";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$range = array("a", "b", "c", "d");
$i = 0;
echo "<table class='tbr' id='tb$id'>";
while($row = $result->fetch_assoc()) {
$data = array_reduce($row, function($carry, $value) {
$carry[] = "<td dbval='{$value}'>{$value}</td>";
return $carry;
}, []);
echo '<tr><td class="tbe">' . $range[$i] . '</td>'
. implode('', $data) . "</tr>\n" ;
$i++;
}
echo "</table>";
}
}
Try this
$range = range('a', 'z');
$i = 0;
echo "<table class='tbr' id='tb$id'>";
while($row = $result->fetch_assoc()) {
$data = array_reduce($row, function($carry, $value) {
$carry[] = "<td dbval='{$value}'>{$value}</td>";
return $carry;
}, []);
$temp = ($i % 26);
$i++;
echo '<tr><td class="tbe">'.$range[$temp].'</td>'. implode('', $data) . "</tr>\n" ;
}
public function select(){
$rows = [];
$connection = $this->connect();
$result = $connection->query("SELECT username FROM users");
while ($row = $result->fetch_assoc()){
$rows[] = $row;
}
$userlist = 0;
foreach($rows as $username){
$userlist .= $username['username'];
}
$get_rankings = [1,2,3,4];
$get_image_path = "images/";
$total = 0;
for ($x = 0; $x < count($get_rankings); $x++){
$total = $get_rankings[$x];
$path .= "<img src = '" . $get_image_path . $total . ".png'>\n" . $userlist . "<br/>";
// echo "<span class = 'align-down'>{$path}";
// echo "<p class = 'user-name'> {$rows['0']}</p>";
// echo "</span>";
}
echo $path;
}
I'm trying to output a simple ranking but using the number index as images to display them.
In the past i've tried to do something similar but couldn't figure out how to match player it with images on the side.
The output im getting is this:
It's outputting each entry 4 times(I get why, its in a loop) but I can't figure out the correct solution to write it outside of a loop or properly
The desired output is:
My DataBase reads as:
[id][username][password]
If there is an easier solution, i'm all ears. I don't know how to approach this.
There's no need for $userlist. Output the username from $rows[$x].
$path = "";
$max = min(count($rows), count($get_rankings));
for ($x = 0; $x < $max; $x++){
$total = $get_rankings[$x];
$path .= "<img src = '" . $get_image_path . $total . ".png'>\n" . $rows[$x]['username'] . "<br/>";
// echo "<span class = 'align-down'>{$path}";
// echo "<p class = 'user-name'> {$rows['0']}</p>";
// echo "</span>";
}
Please help me I have just pulled images from gallery using PHP. But now I just want to display two images in one row. Return it in $content that I have specified.
Please help me.
function CreateGalleryImages($types) {
$galleryArray = array();
$galleryModel = new GalleryModel();
$galleryArray = ($galleryModel -> getGalleryByTypes($types));
$result = "";
foreach ($galleryArray as $gallery) {
for ($i = 0; $i < 2; $i++){
$result = "<td>"
."<img runat = 'server' src = 'http://localhost/schoolwb/event/$gallery' height=500 width=500 />"
."</td>";
}
$result = "<tr>.$result.</tr>";
}
return "<table class = 'GalleryTable'> . $result . </table>";
}
You have to concatenate the result and to store in $result.
Try This
<?php
function CreateGalleryImages($types){
$galleryArray = array();
$galleryModel = new GalleryModel();
$galleryArray = ($galleryModel->getGalleryByTypes($types));
$result = "";
foreach ($galleryArray as $gallery){
$cl = "";
for ($i = 0;$i<2;$i++){
$cl .= "<td>"
."<img runat = 'server' src = 'http://localhost/schoolwb/event/$gallery' height=500 width=500 />"
."</td>";
}
$result .= "<tr>.$cl.</tr>";
}
return "<table class = 'GalleryTable'>.$result.</table>";
}
?>
I got previousRow of record using this code
<?php
$previousRow = array();
while ($temp = mysql_fetch_row($res2))
{
echo "<br>currentRow:".$temp[1];
echo "previousRow:".$previousRow[1];
$previousRow = $temp;
}
?>
oupout
currentRow:1previousRow:
currentRow:5previousRow:1
currentRow:6previousRow:5
currentRow:7previousRow:6
currentRow:8previousRow:7
How can I check the value of the next row replaced by Previous Row ?
Any help would be grateful.
If I get you correctly, then something like this would help?
$previousRow = array();
$currentRow = mysql_fetch_row($res2);
while ($currentRow) {
$nextRow = mysql_fetch_row($res2);
echo "<br>currentRow:".$currentRow[1];
echo "previousRow:".$previousRow[1];
echo "nextRow:".$nextRow[1];
$previousRow = $currentRow;
$currentRow = $nextRow;
}
Please try code given below.
$res = array();
while ($result = mysql_fetch_row($r)) {
$res[] = $result;
}
echo "<pre>";
foreach($res AS $index=>$res1){
echo "Current".$res1[1];
echo " Next" . $res[$index+1][1];
echo " Prev" . $res[$index-1][1]; echo "<br>";
}
thanks
I'd collect all the rows first, then walk through them with a for:
<?php
$rows = array();
while ($temp = mysql_fetch_row($res2))
{
$rows[] = $temp;
}
$rowCount = count($rows);
for ($i = 0; $i < $rowCount; $i++) {
echo "<br>currentRow:".$rows[$i][1];
if ($i > 0) {
echo "previousRow:".$rows[$i - 1][1];
}
if ($i + 1 < $rowCount - 1) {
echo "nextRow:".$rows[$i + 1][1];
}
}
?>
I have the foillowing PHP code, but I can't get it to work?
This is the main PHP file:
function get_data() {
$query = 'SELECT title, article FROM submissions';
$result = mysql_query($query);
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
++$i;
$row['i'] = $i;
$row['title'] = limittext($row['title'], 15);
}
return $row; //perhaps because $row is not return all?
}
$data = get_data();
require('template/data.inc.php');
?>
and this is template/data.inc.php:
<?php
foreach ($data as $value):
echo $data['i'].'<br>';
echo $data['title'].'<br>';
echo $data['article'].'<br>';
endforeach;
?>
template/data.inc.php is meant to output something like:
1 How to get your site on Google?
Text...
2 Secrets of SEO Revealed
Text...
My guess is get_data() is not returning the array() in a form which is supported within the foreach? - as its currently giving an error.
Here is your problem:
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
++$i;
$row['i'] = $i;
$row['title'] = limittext($row['title'], 15);
}
On every iteration, $row is being reset to current record, and in the end mysql_fetch_assoc will turn it to FALSE. You have to put each $row into auxiliary array and return it as whole resultset:
$i = 0;
$returnArray = array();
while ($row = mysql_fetch_assoc($result)) {
++$i;
$row['i'] = $i;
$row['title'] = limittext($row['title'], 15);
$returnArray[] = $row;
}
return $returnArray;
ANd in your template use $value to get details for each row:
foreach ($data as $value):
echo $value['i'].'<br>';
echo $value['title'].'<br>';
echo $value['article'].'<br>';
endforeach;
The way in which you're building $row and returning it
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
++$i;
$row['i'] = $i;
$row['title'] = limittext($row['title'], 15);
}
return $row;
}
You'll only EVER have the LAST iteration of $row set ... if that's what you want? In other words, $row will only continue a single value.
Your loop continues until $row has an value that is evaluated as false:
while ($row = mysql_fetch_assoc($result)) {
and then you return $row so you always return null or false!
replace
$row['title'] = limittext($row['title'], 15);
with
$row['title'] = limittext($row['title'], 15);
$result[]=$row;
and
return $row;
with
return $result;