Log in

View Full Version : PHP image gallery problems - please help



zchrykng
06-27-2011, 02:58 PM
Hi everyone I have been working on a php script for populating an image gallery. I have been having strange problems as have not been able to figure out what I am doing wrong. Any help would be great.

This is what I get from running the script.

query($query); $num_results = $result->num_rows; if ($num_results < 8) { $grid_fill = $num_results; } else { $grid_fill = 8; } $num_pages = $num_results / 8; echo '
'; for ($counts = 0; $counts < $grid_fill; ++$counts) { $row = $result->fetch_assoc(); $restitle = stripslashes($row['imagenm']); $resdsc = stripslashes($row['imagedsc']); $resindex = ; echo '
$restitle $resdsc

'; } echo '
'; ?>

While here is the code that I wrote for the script... passwords and such removed

<?php
$pagenum = $_GET['page'];
$category = $_GET['category'];

if ($pagenum == NULL) {
$pagenum = 1;
}



@ $db = new mysqli('server', 'user', 'password', 'database');
if (mrsqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

$query = 'select imageid, imagenm, imagedsc from portfolio';
if ($category != NULL ) {
$query = $query . 'where imagetp = ' . $category;
}
$query = $query . ' order by imagedtad';
$result = $db->query($query);

$num_results = $result->num_rows;

if ($num_results < 8) {
$grid_fill = $num_results;
}
else {
$grid_fill = 8;
}

$num_pages = $num_results / 8;

echo '<div class=\"content\">
<ul class=\"grid group\">
';

for ($counts = 0; $counts < $grid_fill; ++$counts) {
$row = $result->fetch_assoc();
$restitle = stripslashes($row['imagenm']);
$resdsc = stripslashes($row['imagedsc']);
$resindex = ;
echo '<li>
<div class=\"infooverlay\">
<div class=\"valign\">
<div class=\"info\">
<span class=\"title\">$restitle</span>
<span class=\"dsc\">$resdsc</span>
</div>
</div>
</div>
<a href=\"$resindex\" >
<img alt=\"$restitle\" src=\"portfolio/thumbnails/$resindex.png\">
</a>
</li>';
}

echo '
</ul>
</div>';

?>

fastsol1
06-27-2011, 03:14 PM
The main things I see that could be causing the query to fail are on these lines

$query = 'select imageid, imagenm, imagedsc from portfolio';
if ($category != NULL ) {
$query = $query . 'where imagetp = ' . $category;
}
$query = $query . ' order by imagedtad';
You should be using capitals for the query SELECT WHERE, FROM and ORDER BY. Also it is best to put `backticks` around the table names and table columns (not single quotes, backticks are above the tab key). Then if $category is not an integer you need to put single quotes around it.
On this line

$query = $query . 'where imagetp = ' . $category;
you need to add a space between the ' and where, like this

$query = $query . ' where imagetp = ' . $category;
Otherwise when it is joined to the first part of the query it is reading it like this - from portfoliowhere imagetp = because you don't have the backticks.
I did notice that you misspelled the mysqli on this line, although that is not your real issue.

if (mrsqli_connect_errno())
Then after this line

$result = $db->query($query);
Put this

echo mysqli_error();
I have never used mysqli so I don't know for sure that what I just said is 100% correct or it might be mysql_error().
Putting that line in will tell you if the query fails and why, then once the full code is perfected you can remove it for the live site.

zchrykng
06-27-2011, 04:22 PM
Thanks for the quick response. I tried your suggestions, and could not get it to work. So I have decided to do what I should have done from the beginning, build the script piece by piece testing after each addition. Rather than writing the entire thing in one shot. If I can get it working, or need more help I will post here again.

Thanks,
Zach

bluewalrus
06-27-2011, 06:27 PM
Only escape double quotes if you are using double quotes to encapsulate the string. In double quotes variables will be parsed however in single quotes they will not.

so this


echo '<li>
<div class=\"infooverlay\">
<div class=\"valign\">
<div class=\"info\">
<span class=\"title\">$restitle</span>
<span class=\"dsc\">$resdsc</span>
</div>
</div>
</div>
<a href=\"$resindex\" >
<img alt=\"$restitle\" src=\"portfolio/thumbnails/$resindex.png\">
</a>
</li>';

should be



echo "<li>
<div class=\"infooverlay\">
<div class=\"valign\">
<div class=\"info\">
<span class=\"title\">$restitle</span>
<span class=\"dsc\">$resdsc</span>
</div>
</div>
</div>
<a href=\"$resindex\" >
<img alt=\"$restitle\" src=\"portfolio/thumbnails/$resindex.png\">
</a>
</li>";

or


echo '<li>
<div class="infooverlay">
<div class="valign">
<div class="info">
<span class="title">' . $restitle . '</span>
<span class="dsc">' . $resdsc .'</span>
</div>
</div>
</div>
<a href="' . $resindex . '">
<img alt="' . $restitle . '" src="portfolio/thumbnails/$resindex.png">
</a>
</li>';

fastsol1
06-27-2011, 11:01 PM
Oh yeah, I saw that before but forgot to mention it in my post, oh well.