Jump to content

PHP Program help please.


SpeedCrazy

Recommended Posts

I am making myself an image gallery from scratch and as i am planning to build an image uploader into it so i have to has a dynamic image loader to make sure all images are loaded. So i have a working mysql db that stores image # album # comments title and date uploaded.

So far my program was able to load all that info and display it, so now i am trying to make it take the album and img numbers and form the <img /> src url.

I need some help getting it to work as right now it is not working, i get an "unexpected T_string parse error"

PHP Code

<?php

$conn = mysql_connect("localhost","portfolio","portfolio") or die(mysql_error());
mysql_select_db("portfolio");

$sqlA = "SELECT album FROM pics";
$resultA = mysql_query($sqlA, $conn) or die(mysql_error());

while($row=mysql_fetch_assoc($resultA)){
foreach($resultA as $album){
	$sqlI = "SELECT img FROM pics WHERE album $resultA";
	$resultB = mysql_query($sqlI, $conn) or die(mysql_error());

	print "<div>"<img src="http://localhost/PortfolioSite/img/content/full/$resultB.jpg"/>"</div>"
} //end foreach
}// end while

?>

Any ideas?

Thanks

 

EDIT: :pfp: I could just add the url to the db. Will that work ya think?

Edited by SpeedCrazy

Share this post


Link to post
Share on other sites

A quick look makes it seem your print statement has the quotes messed up. try

 

print '<div><img src="http://localhost/PortfolioSite/img/content/full/'.$resultB.'.jpg"/></div>'

 

actually I'm not sure it likes the php variable in the middle of the url either.

Edited by flareback

Share this post


Link to post
Share on other sites

The foreach doesn't work. It tells me it should be an array but it is just a single value. How would i fix that?Is there an alternative to foreach that deals with single values?

Share this post


Link to post
Share on other sites

Okay, on a closer look you're using the result from the queries wrong. Those are resources being returned not text. I would try something that looks more like the following. I took out the foreach and added a while statement for your second query

 

<?php

$conn = mysql_connect("localhost","portfolio","portfolio") or die(mysql_error());
mysql_select_db("portfolio");

$sqlA = "SELECT album FROM pics";
$resultA = mysql_query($sqlA, $conn) or die(mysql_error());

while($row=mysql_fetch_assoc($resultA)){
	$sqlI = "SELECT img FROM pics WHERE album = ". $row['album'];
	$resultB = mysql_query($sqlI, $conn) or die(mysql_error());
	while($rowB = mysql_fetch_assoc($resultB)){
	   print '<div><img src="http://localhost/PortfolioSite/img/content/full/'.$rowB['img'].'.jpg"/></div>';
               }
}// end while

?>

Share this post


Link to post
Share on other sites

Okay, i think this should add titles but it throws a very unusual error.

<?php

$conn = mysql_connect("localhost","portfolio","portfolio") or die(mysql_error());
mysql_select_db("portfolio");

$sqlA = "SELECT album FROM pics";
$resultA = mysql_query($sqlA, $conn) or die(mysql_error());

while($row=mysql_fetch_assoc($resultA)){
               $sqlI = "SELECT img FROM pics WHERE album = ". $row['album'];
               $resultB = mysql_query($sqlI, $conn) or die(mysql_error());

               $sqlT = "SELECT title FROM pics WHERE album =". $row['album']."AND img=" .$resultB['img'];
			$resultC = mysql_query($sqlT, $conn) or die(mysql_error());

			$rowC=mysql_fetch_assoc($resultC);

               while($rowB = mysql_fetch_assoc($resultB)){
                  print '<div id="album'.$row['album'].'"><img src="http://localhost/PortfolioSite/img/content/album'.$row['album'].'/full/'.$rowB['img'].'.jpg"/><h3>'. $rowC['title'] .'</h3></div>';
               }
}// end while


 ?>

it says

div #main 4 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'img=' at line 1

This program is quite literally the only thing on the page so i haven't a clue what it means.

Share this post


Link to post
Share on other sites

you can select more than one item from a table at a time. try this

 

$sqlI = "SELECT img, title FROM pics WHERE album = ". $row['album'];

then in your while loop you can access it with $rowB['title']. That will help cut down on querying the database. Try checking out some tutorials on using the select statement. Here is a pretty simple example that should get you what you want.

Share this post


Link to post
Share on other sites

But i need to separate them.

I want to have the image and then below it the title and comments. Though i have decided it might be easier to have the <img src="htt://etc." /> all in the database requiring just 1 query.

Edited by SpeedCrazy

Share this post


Link to post
Share on other sites

But i need to separate them.

I want to have the image and then below it the title and comments. Though i have decided it might be easier to have the <img src="htt://etc." /> all in the database requiring just 1 query.

You can separate it.

$sqlI = "SELECT img, title FROM pics WHERE album = ". $row['album'];
$resultB = mysql_query($sqlI, $conn) or die(mysql_error());

while($rowB = mysql_fetch_assoc($resultB)){
  print '<div id="album'.$row['album'].'"><img src="http://localhost/PortfolioSite/img/content/album'.$row['album'].'/full/'.$rowB['img'].'.jpg"/><h3>'. $rowB['title'] .'</h3></div>';
}

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...