$selectra_result = mysql_query("SELECT * FROM `my_selectra` WHERE user_id='$user_id'") or die(mysql_error());
while($row = mysql_fetch_array( $selectra_result )) {
$product_id = $row['product_id'];
$namep[] = $product_name= $row['product_name'];
$linkp[] = $product_permalink = $row['product_permalink'];
$imgpp[] = $product_image_path = $row['product_image_path'];
}
foreach($namep as $pname => $prodname){
foreach($linkp as $plink => $prodlink) {
foreach($imgpp as $ppath => $prodpath) {
$res1 = "<a href='$prodlink'><p>$prodname</p></a>";
$res2 = "<a href='$prodlink'><img src='$prodpath'/>";
}
}
}
echo $res2.''.$res1;
}
I want to display data from my_selectra table using foreach, unfortunately it appears only one data when echo.
Your code is pretty convoluted... You don't need 3 arrays to grab the data you want, nor do you need a triple-nested foreach loop structure. Try something like this:
$results = array();
while($row = mysql_fetch_array( $selectra_result))
{
$product_id = $row['product_id'];
$results[] = array( $row['product_name'], $row['product_permalink'], $row['product_image_path']);
}
$res1 = $res2 = '';
foreach( $results as $row)
{
$res1 .= "<a href='" . $row[1] . "'><p>" . $row[0] . "</p></a>";
$res2 .= "<a href='" . $row[1] . "'><img src='" . $row[2] . "'/>";
}
echo $res1 . $res2;
You keep setting $res1 and $res2 with each loop.
I think what you meant to do is $res1 = $res2 = ''; before the loop, then $res1 .= '...'; $res2 .= '...'; inside the loop.
Your echo is in the wrong scope. Place it directly underneath the $res2 variable.
Your code has a lot of double-declarations and variable shifting. I'm not sure you correctly understand basic programming paradigms. You should probably go back to basics, to be honest.
$selectra_result = mysql_query("SELECT * FROM `my_selectra` WHERE user_id='".intval($user_id)."'") or die(mysql_error());
while($row = mysql_fetch_array( $selectra_result )) {
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$product_image_path = $row['product_image_path'];
$product_permalink = $row['product_permalink'];
echo "<a href='$product_permalink'><p>$product_name</p></a>";
echo "<a href='$product_permalink'><img src='$product_image_path'/>";
}
This is closer to what you are after.
Your query most probably selects only one row from the database (if the id column is unique).
And why a loop (foreach) for each array? Considering that all arrays are created (although this is not very clear, because you don't define the variables in the code provided) in the same loop (while) there is no need to do that...
Related
i have this php code that fetches images from the database using the userid, then displays all the images in a list form. im trying to paginate the images, in a limit of 5 items per page. but the code is showing only the first page, without a link to the other pages. here's my php code
<?php
include 'connect.php';
$Category = " ";
$query = "SELECT Img_dir, Caption, Category FROM images WHERE Category = '". $_REQUEST['Category'] ."' AND user_id = '". $_SESSION['user_id'] ."' LIMIT 0,5";
$result = mysqli_query($conn,$query);
while ($row=mysqli_fetch_array($result)){
$image = $row["Img_dir"];
$Caption= $row["Caption"];
$Category = $row["Category"];
echo "<dl>";
echo "<dd>$Category    <img src='base64_encode($image)' />   $Caption<dd>";
echo "</dl>";
}
//number of total pages available
$results_per_page = 10;
$number_of_results = mysqli_num_rows($result);
echo $number_of_pages = ceil($number_of_results / $results_per_page);
echo "<br>"; echo "<br>";
for($r=1;$r<=$number_of_pages;$r++)
{
?><?php echo $r." "; ?><?php
}
?>
You can try this:
Change your query (use prepare statments):
$query = "SELECT Img_dir, Category FROM images WHERE user_id = ? AND Category = ? ";
As for the structure of your data.
$results = [];
while ($row = $result->fetch_assoc()){
$key = $row['Category'];
if(!isset($results[$key])) $results[$key] = [];
$results[$key][] = $row['Img_dir ']; //['Category' => [Img_dir,Img_dir, ...]]
}
And your HTML. I would use a description list or dl as it has a nice place for the title:
foreach($results as $Category => $image){
echo "<dl>";
echo "<dt>$Category</dt>";
foreach($data as $row){
echo "<dd><img src='base64_encode($image)' /><dd>";
}
echo "</dl>";
}
Untested.
The order will probably be all wanky, so you can use ksort on it. Simply
ksort($results);
Before the foreach loops.
Cheers.
What I need to make happen:
I am populating divs with data from mySQL based on the item ID. I need to create variables to be called into the divs such as $tip_ + $id which results in:
$tooltip_1
$tooltip_2
$tooltip_3
What I have done so far:
$query = 'SELECT * FROM top_tips WHERE type = "aw"';
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_row($result)) {
$tip_ID = $row[0];
$tip_text = $row[2];
$tip_link = $row[3];
$tooltip = "<div class='tooltip-close'><a href='#' class='close'>×</a></div>" .$tip_text . " <a href='" .$tip_link . "'>read more ...</a>";
echo $tooltip;
}
Where I am stuck:
What is the most efficient way to output $tooltip + $tip_id so the variables can be called into the respective divs?
You would do it like this:
while ($row = mysqli_fetch_row($result)) {
$tip_ID = $row[0];
$tip_text = $row[2];
$tip_link = $row[3];
$tooltip = "tooltip_{$tip_ID}";
$$tooltip = "<div class='tooltip-close'><a href='#' class='close'>×</a></div>" .$tip_text . " <a href='" .$tip_link . "'>read more ...</a>";
echo "tooltip_{$tip_ID}";
}
That will give you variables such as $tooltip_1, $tooltip_2 and so on. That said, I recommend you use an Array for this.
Added For foreach for your Code..
<?php
$query = 'SELECT * FROM top_tips WHERE type = "aw"';
$result = mysqli_query($link, $query);
while ($results = mysqli_fetch_row($result)) {
foreach($results as $row){
$tip_ID = $row[0];
$tip_text = $row[2];
$tip_link = $row[3];
$tooltip = "<div class='tooltip-close'><a href='#' class='close'>×</a></div>" .$tip_text . " <a href='" .$tip_link . "'>read more ...</a>";
echo $tooltip;
}
}
?>
Hope you need this solution.. im confused in your question.
You can create an array of $tooltips like so:
$tooltip[] = "<div class='tooltip-close'><a href='#' class='close'>×</a></div>" .$tip_text . " <a href='" .$tip_link . "'>read more ...</a>";
It seems that you care about efficiency and how to concatenate your $tooltip_ and $tip_ID. First about efficiency, I always learn : When you are fetching results through a loop, never creates variables. Always outside !
In your case, you could create it like this before fetching the results :
<?php
$tooltip_array = array(); //Future array containing your $tooltip_1, $tooltip_2...
$query = 'SELECT * FROM top_tips WHERE type = "aw"';
$result = mysqli_query($link, $query);
Then, in your loop you can push each tooltip onto your array like this :
while ($row = mysqli_fetch_row($result)) {
array_push($tooltip_array,"<div class='tooltip-close'><a href='#' class='close'>×</a></div>" .$row[2] . " <a href='" . $row[3] . "'>read more ...</a>");
}
Finally you can echo this out with a foreach loop :
// Iterating through the values
foreach($tooltip_array as $value)
{
echo $value;
}
I wanted to post the working solution I ended up using for anyone reading this thread.
$query = 'SELECT * FROM tool_tips WHERE tip_type = "aw"';
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_row($result)) {
$tooltip_array[$row[0]] = "<div class='tooltip-close'><a
href='#' class='close'>×</a></div>" .$row[2]. "<a href='" .$row[3]. "'>read more ...</a>";
}
Then I call the individual tooltips like:
<?php echo $tooltip_array[12];?>
I'm trying to echo a bit of html within a while loop. I'm getting my stuff with PDO and PDO_ASSOC.
This is what I have:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
$text = "ID: ' . $book_id . '";
}
return $text;
But it gives me only one row of the table. I even tried fetchAll, but it gives me nothing.
So making the assumption that the only element ever seen is the last element it is because what your are returning is being overwritten each loop. There are a few options to resolve this. The simplest is:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$text = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
//String concatenation of text will
//give you one big string at the end to return.
$text .= "ID: '{$book_id}'";
}
return $text;
However this will not work well with your real bootstrap html. You need to make sure that the columns add up right.
You will need something a bit more intuitive
Using the actual code it would look something like
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$bookEcho = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$bookEcho[] = '<div class="col-md-3">
<div class="thumbnail">
<span>' . $book_title . '</span>
<img src="' . $book_image . '">
<div class="book-options">
<span>Bewertung</span><br/>
' . $stars . '
Jetzt lesen
</div>
</div>
</div>';
}
return $bookEcho;
Now in your function what ever it is you can do something like (this is not the most elegant thing I have ever written but should get the job done):
$cols = 4;
$colCount = 1;
foreach ($bookEcho as $book){
if($colCount == 0){//create a row}
echo $book;
$coolCount++;
if($colCount == 0){end a row}
if($colCount == 4){ $colCount = 0;}
}
The problem is that you're overwriting your values in your while loop. The loop is being executed once for each entry in the database, but only the last one will be returned. Instead you want to use arrays:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$books = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book['id'] = $row['id'];
$book['title'] = $row['title'];
$book['image'] = $row['image'];
$book['amz'] = $row['amazon'];
$book['desc'] = $row['description'];
$book['rating'] = $row['rating'];
$book['date'] = $row['date'];
$book['author'] = $row['author'];
$book['categorie'] = $row['categorie'];
$book['text'] = "ID: ' . {$book['id']} . '"; // << Not sure if this is what you actually want. If not, adjust accordingly.
// Append the above values to the $books array
$books[] = $book;
}
return $books;
Use a foreach:
$books = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($books as $book){
$book_id = $book['id'];
$book_title = $book['title'];
// ...
}
I have a mysql table in which contains video titles, video embed html, video description, and video thumbnails. I want it to output the first entry as;
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}
and after that, I would like it to output the next five entries as
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
however I have no idea how to do this. I am kind of a newb when it comes to mysql. Any help would be appreciated.
I have looked at other similar questions but none of them really fit the bill.
Poor man's solution would be something like:
$result = mysqli_query($con,"SELECT * FROM entries");
$first = true;
while($row = mysqli_fetch_array($result)) {
if ($first) {
echo $row['title'], $row['html'], $row['desc'];
$first = false;
continue 2;
}
echo $row['title'], "<a href='{$row[id]}'><img src='{$row[thu]}'></a>";
}
(As explained in PHP chatroom)
Since mysqli_fetch_array() gets you a row, you can use it before the while() without a problem like this
$result = mysqli_query($con,"SELECT * FROM entries");
$row = mysqli_fetch_array($result);
echo $row['title'];
//etc.
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
Track it with a sentinel value
$i = 0;
while($row = mysqli_fetch_array($result)) {
if($i == 0){
//first iteration
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}else{
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
$i++;//increment value
}
I have a query to bring results from my database. It works... until there are more than 2 results that it, then it just repeats some results before adding in new ones.
I know it will be because my query is fairly poor, can anyone advise me?
The idea is
connect to database with photo links
get the default user picture as $profile_main
join the words "photo_" with the default picture number and call it
$answer (ex: column 'photo_1' in database)
now check the database again and get the results for $answer and
output all information from that database column.
$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
{
$profile_main = $row['default'];
$answer = "photo_" . $profile_main;
$result2 = mysqli_query($con,"SELECT $answer FROM tbl_photos");
while($row = mysqli_fetch_array($result2))
{
echo "<img src=\"" . $row[0] . "\">";
}
}
mysql_fetch_row returns numerical indexes instead of column names (so ['default'] just won't work)...
This is how I would do it if I'm understanding you correctly:
$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_assoc($result))
{
$answer = $row['photo_'.$row['default']];
echo "<img src=\"" . $answer . "\">";
}
Anyway, this is assuming default and photo_x are in the same row.
If you want only one result for a photo then you can use LIMIT like this
SELECT $answer FROM tbl_photos LIMIT 1
First, both loops you set same $row variable. Use 2 different variable names so that the results don't get mixed up.
Second issue is that you have you have 2 loops , so it will show all results each time. You need to break in the second loop. Like this:
$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
{
$profile_main = $row['default'];
$answer = "photo_" . $profile_main;
$result2 = mysqli_query($con,"SELECT $answer FROM tbl_photos");
while($row2 = mysqli_fetch_array($result2))
{
echo "<img src=\"" . $row2[0] . "\">";
break;
}
}
Or by using only one query, it would be much more efficient:
$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
{
$profile_main = $row['default'];
$answer = "photo_" . $profile_main;
echo "<img src=\"" . $row[$answer] . "\">";
}
You only require 1 query.
TRY
$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
{
$photo = "photo_" .($row['default'];
echo "<img src=\"" . $photo . "\">";
}