while inside of another while in PHP using MySQL - php

I need to do the following - get the list of all artists from the database and print them on a page. But besides of that, I need to make another query to the "albums" table and get the albums of each artist and print the images under each artist description. The code I've written is as follows:
require('db.php');
$query = "SELECT * FROM artists";
$result = mysql_query($query);
$artist_id = $result[id];
$artist_name = $result[name];
$artist_surname = $result[surname];
$artist_email = $result[email];
$artist_about = $result[about];
$artist_photo = $result[photo];
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result)) :
print "<div class='row'>";
print "<div class='artists_left'>";
print "<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>";
print "</div>";
print "<div class='artists_right'>";
print "<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>";
print "<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>";
print "<ul class='artists'>";
$albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
$albums_result = mysql_query($albums_query);
$album_id = $albums_result[id];
$album_photo = $albums_result[title_photo];
$album_dirname = $albums_result[dirname];
while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($result)) :
print "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
endwhile;
print "</ul>";
print "<a href='#' class='seemore_portfolio'>все работы мастера ></a>";
print "</div>";
print "</div>";
endwhile;
mysql_close($connect);
The outer query works fine, but the inner one - does not. Could anybody help me to figure this out?
Thanks in advance.

Change the second $result to $albums_result
You can also consider writing the whole thing like this...
<?
require('db.php');
$query = "SELECT * FROM artists";
$result = mysql_query($query);
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result))
{
$li = '';
$albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
$albums_result = mysql_query($albums_query);
while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($albums_result))
{
$li .= "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
}
echo "<div class='row'>
<div class='artists_left'>
<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>
</div>
<div class='artists_right'>
<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>
<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>
<ul class='artists'>
$li
</ul>
<a href='#' class='seemore_portfolio'>все работы мастера ></a>
</div>
</div>";
}

Related

Display 3 images in a row using php

I want to show 3 images in a row but i cant find the problem in my code.
<div class="pagh">
<?php
$sql = "SELECT * from imagens INNER JOIN users ON imagens.id_user = users.id_user ";
$consulta = mysqli_query($bd, $sql);
$n_linhas = mysqli_num_rows($consulta);
if ($n_linhas!=0) {
for ($i=1;$i<=$n_linhas;$i++){
$dados = mysqli_fetch_array($consulta);
echo '<div class="row">';
echo '<div class="column">
<img src="user_img/' . $dados["filename"] . '" width="150%">';
echo '</div>';
echo '</div>';
}//for
}//if
?>
code img
change your code like this :
echo '<div class="row">;
for($i = 1; $i<$n_linhas;$i++){
echo '<div class="column">
<img src="...">
</div>';
}
echo '</div>';
In your code, at each $i your write a new row
Inspect your code with right click to see what happend

How do I tweet out my image like I do when I share with facebook?

I managed to find a way with facebook, but no so much with Twitter. The only progress I got is only with text, but I want it with my image that I shared.
Here is the code:
<?php
$db = mysqli_connect("localhost", "root", "", "photos");
$sql = "SELECT * FROM images ORDER BY id DESC";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<a href='uploads/".$row['image']."' data-caption='".$row['text']."'> ";
echo "<img id='img_div' src='uploads/".$row['image']."'/>";
//echo "<p id='img_div'>".$row['desc']."</p>";
echo "</a>";
echo"<p id='p_div'>Share</p>";
echo "<a href='http://www.facebook.com/sharer/sharer.php?u=www.example.com/uploads/".$row['image']."'>
<i id='share_div' class='fa fa-facebook fa-lg' aria-hidden='true'></i></a>";
echo "<a id='tweet_div' href='http://www.twitter.com/share?text=Check out this image from "Quotin"!&url=http://www.eample.com/uploads/".$row['image']."'&via=_Quotin_'>Tweet</a>";
}
?>

php and mysql, how to sort inside while loop

I need to sort while loop by row named 'sort' or $udaj[5] My code below.
<?php
$query = "SELECT * FROM yees where category = 1";
$vysledek = mysqli_query($my_link, $query);
while ($udaj = mysqli_fetch_array($vysledek)):
echo "<a href='detail.php?id=" . $udaj[0] . "' class='yee'>";
echo "<img src='" . $udaj[3] . "' alt='' class='avatar'>";
echo "<div class='basic-info'>";
echo "<div class='name'>" . $udaj[1]."</div>";
echo "<div class='tagline'>" . $udaj[6] . "</div>";
echo "</div>";
echo "<img src='" . $udaj[4] . "' alt='' class='cover'>";
echo "<div class='clear'> </div>";
echo "</a>";
endwhile;
?>
I tried this, but doesnt work.
$query = "SELECT * FROM yees where category = 1 ORDER BY sort ASC";
Could the problem be that my col is called "sort" which is not allowed since its a keyword??
You should go back to using the SQL to sort for you. I don't think there's anything wrong there.
It is also cleaner to use the proper row names, as mysqli_fetch_array will return the values in an associated array as well as a numbered index array.
Also print HTML out in heredoc format; its much easier to read than multiple echo statements
echo <<<EOF
<a href='detail.php?id={$udaj['col0']}' class='yee'>
<img src='{$udaj['col3']}' alt='' class='avatar'>
<div class='basic-info'>
<div class='name'>{$udaj['col1']}</div>
<div class='tagline'>{$udaj['col6']}</div>
</div>
<img src='{$udaj['col4']}' alt='' class='cover'>
<div class='clear'> </div>
</a>
EOF;

Next and previous links and ID displayed in the URL for sharing

I have a database with a few images already set, I would like to have the url display an ID from each query as the user hits next. The user should be able to share the URL and paste it into their browser, the url should pull that unique ID from the query. The issue i am having is every time i paste a url, I get a random image and not the image that is in the ID. I'm at a loss here and im not sure what to do :( here's the code I have so far.
<?php
if (isset($_GET['id'])) {
include("PHP/db.php");
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
if($_GET['next4']) {
echo 'HELLO THIS IS THE NEXT IF METHOD';
include("PHP/db.php");
$query = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $ID ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
Try this:
<?php
include("PHP/db.php");
$query2 = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result2 = mysqli_query($dbc, $query2);
$rand_row = mysqli_fetch_array($result2);
$rand_id = $rand_row ['ID'];
if (!isset($_GET['id'])) {
$_GET['id'] = $rand_id;
}
if (isset($_GET['id'])) {
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $rand_id; ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
I changed:
-link to next is now id=rand
-changed your code to give me a "rand ID" and its already defined on the href of the page you load
It will go inside condition (next == true).
Make sure that your variables are initialized before use.

Php foreach echo issue

I have this code which produces me a number which is equal to the number of id's i've got in my database of star rating system.
This code generates me a five star voting for each id i've got, but the problem is, it generates them all in a div, while i need them specifically in different div's. let's suppose i print out in a div information for each hostess i've got, i print out their photo and name with the following code:
$sql =" select * from hostess";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "<div id='photo'>";
echo "<div id='picture'>";
echo "<div id='scotch'><img src='images/Scotch.png'></div>";
echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
echo "</div>";
echo "<div id='text'>";
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
echo "</div>";
echo "</div>";
echo "<div id='photo2'>";
echo "<div id='picture'>";
echo "<div id='notes'>";
echo '<form action="index.php" method="post" >';
echo "<label>Notes</label></br><textarea>".$row['courrent_occupation'] . "</textarea></br>";
echo '<input type="submit" value="edit" name="edit"></div>';
echo "</div>";
echo "<div id='notes'>";
echo "<label>profile</label></br><textarea>".$row['profile_en'] . "</textarea>";
echo "</div>";
echo "</div>";
}
?>
</div>
Now, i've got this other php which generates me all the star ratings for all hostess id's
<?php
// include update.php
include_once 'update.php';
// get all data from tabel
$arr_star = fetchStar();
?>
<?php
// start looping datas
foreach($arr_star as $star){ ?>
<h2>Star Rater - <?php echo $star['id'];?></h2>
<ul class='star-rating' id="star-rating-<?php echo $star['id'];?>">
<?php /* getRating($id) is to generate current rating */?>
<li class="current-rating" id="current-rating-<?php echo $star['id'];?>" style="width:<?php echo getRating($star['id'])?>%"><!-- will show current rating --></li>
<?php
/* we need to generate 'id' for star rating.. this 'id' will identify which data to execute */
/* we will pass it in ajax later */
?>
<span class="ratelinks" id="<?php echo $star['id'];?>">
<li>1</li>
<li>1.5</li>
<li>2</li>
<li>2.5</li>
<li>3</li>
<li>3.5</li>
<li>4</li>
<li>4.5</li>
<li>5</li>
</span>
</ul>
<?php } ?>
What i need is to assign each hostess profile i print their system rating.
I try to insert the foreach inside the first script but it then shows me just one profile, not all profiles.
The fetchstar() code is:
function fetchStar(){
$sql = "select * from `hostess`";
$result=#mysql_query($sql);
while($rs = #mysql_fetch_array($result,MYSQL_ASSOC)){
$arr_data[] = $rs;
}
return $arr_data;
}
First, you probably shouldn't use SELECT *. That aside I would combine the two queries you have to return a multidimensional array with MySQL and then use nested for each loops to echo out the data you want.
Someone answered a similar question for me here.
Looping through MySQL left join in php vs. 2 separate queries
$sql =" select * from hostess";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
if ($lastID <> $row['id']) {
$lastID = $row['id'];
$hostess[$lastID] = array('id' => $row['id'],
'first_name_en' => $row['first_name_en'],
etc
'arr_star' => array() );
}
$hostess[$lastID]['arr_star'][] = array('star_id' => $row['star_id'] etc);
}
Then you would use nested for each statements
for each($row as $rows){
//echo your hostess information
for each ($arr_star as $star){
//echo your star rating information
}
}

Categories