PDA

View Full Version : Problem calling images from MySQL


cocoonfx
12-30-2010, 04:41 PM
Hi Chaps


I have struggled to get the last part of my project to work. I have a MySQL database with images which are assigned to groups and pages. I then have a php page where I would like to display the images. Within this page there is a sub navigation. I.e for different events (groups).

http://www.tamworthkickboxing.com/test2.php

You will see if you click on VI you will see a picture of a t-shirt however there is 2 images within the MySQL table and it seems to be only picking the highest id. Also if there is only 1 image it will not display the images at all. I am very rusty with PHP. Can any one point me in the direction to fix this?

Code below

////

<?PHP
include("configure.php");

$select_image_query = mysql_query("select * from images where image_page='Battleground'") or die(mysql_error());
$select_image2_query = mysql_query("select * from images GROUP By image_group") or die(mysql_error());




if($_GET['group'] == $select_image2_query['image_group']){
$populate_data_query = mysql_query("select * from images where image_group = '" . $_GET['image_group'] . "'");
$populate_data_array = mysql_fetch_array($populate_data_query);

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>

<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/swfobject/swfobject.js">
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Tamworth Kickboxing Little Ninjas</title>

<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/swfobject/swfobject.js">
</script>
<script src="Scripts/swfobject_modified.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var flashvars = {};
flashvars.xml = "config.xml";
flashvars.font = "font.swf";
var attributes = {};
attributes.wmode = "transparent";
attributes.id = "slider";
swfobject.embedSWF("cu3er.swf", "cu3er-container", "960", "400", "9", "expressInstall.swf", flashvars, attributes);
//]]>
</script>
<script type="text/javascript" src="js/jquery.js"></script>

<link rel="stylesheet" type="text/css" href="../style-projects-jquery.css" />
<!-- Arquivos utilizados pelo jQuery lightBox plugin -->

<script type="text/javascript" src="js/jquery.lightbox-0.5.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.lightbox-0.5.css" media="screen" />
<!-- / fim dos arquivos utilizados pelo jQuery lightBox plugin -->

<!-- Ativando o jQuery lightBox plugin -->
<script type="text/javascript">
$(function() {
$('#gallery a').lightBox();
});
</script>
</head>

<body>
<div class="main">
<div class="blok_header">
<div class="header">

<div class="clr"></div>



<div class="clr"></div>

<div class="header_text_bg">
<div class="cu3er_resize">
<div id="cu3er-container">
<a href="http://www.adobe.com/go/getflashplayer"><img src=
"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt=
"Get Adobe Flash player" /></a>
</div>
</div>


<div class="menu_resize_bg">
<div class="menu_resize">
<div class="menu">
<ul>
<li><a href="index.html" class="active"><span>Home</span></a></li>
<li><a href="instructors.html"><span>Instructors</span></a></li>
<li><a href="testimonials.html"><span>Testimonials</span></a></li>
<li><a href="prices.html"><span>Prices</span></a></li>
<li><a href="time.html"><span>Timetable</span></a></li>
<li><a href="gym.html"><span>Gym/Dojo</span></a></li>
<li><a href="gallery.html"><span>Gallery</span></a></li>

<li><a href="battleground.html"><span>Battleground</span></a></li>
<li><a href="links.html"><span> Links</span></a></li>
<li><a href="tournaments.html"><span>Tournaments</span></a></li>
<li><a href="contact.html"><span> Contact Us</span></a></li>
</ul>

</div>


<div class="clr"></div>
</div>

<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</div>
<div class="body_resize">
<div class="body">
<div class="main_text">
<h2>Tamworth Kickboxing Academy - T.K.A Team England<br />
<span><strong></strong>Education through Martial Arts</span></h2>

<p>Tamworth Kickboxing Academy (T.K.A.) has various levels of fighters from all ages. Please click on the links below to view the various battleground fights</p>
<p></p>
<div class="submenu_resize_bg">
<div class="submenu_resize">
<div class="submenu">
<ul>
<?php
$i = 0;
while($select_image2_data = mysql_fetch_array($select_image2_query)){
$i++;
?>
<li><a href="test2.php?image_group=<?php echo $select_image2_data['image_group'];?>"><span><?php echo $select_image2_data['image_group'];?></span></a></li>
<?php } ?>


</ul>

</div>


<div class="clr"></div>
</div>

<div class="clr"></div>
</div>


<p>&nbsp;</p>
<div id="gallery">




<p> WRSA Full Contact Champion's</p>
<p>
<?php
$i = 0;
while($select_image_data = mysql_fetch_array($populate_data_query)){
$i++;
?>
<h2><?PHP echo $select_image_data['image_page'];?> - <?PHP echo $select_image_data['image_group'];?></h2>
<a href="images/<?php echo $select_image_data['image_file_name'] ;?>" width="560" height="350" title="<?php echo $select_image_data['image_title'];?>"/>
<img src="images/<?php echo $select_image_data['image_file_name'];?>" width="150" height="150" alt="" />
</a>
<?php } ?>

</p>

<p>


</p>

<p>&nbsp;</p>



</a></div>

</div>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>

</div>


<div class="footer">
<div class="footer_resize">
<p class="leftt"> Copyright Tamworth Kickboxing Academy. All Rights Reserved. <a href="#">Home</a> | <a href="#">Contact</a> | <a href="#">RSS </a></p>
<p class="right"><a href="http://www.cocoonfxmedia.co.uk">Website Managed by Cocoonfxmedia</a></p>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</body>
</html>

jmichae3
01-01-2011, 01:47 AM
people commonly mistakenly use mysql_fetch_array() when they really want to be using mysql_fetch_assoc().

otherwise, without seeing your database contents, it looks golden. maybe somebody else will see something I missed.

jmichae3
01-01-2011, 01:49 AM
I should mention that when you use mysql_fetch_array, the contents of the array are addressable with numbered indexes. with assoc, you can use names.

cocoonfx
01-01-2011, 05:42 PM
Hi

I changed to mysql_fetch_array() and its still not work.

cocoonfx
01-02-2011, 12:02 PM
i have also used mysql_fetch_assoc() and it does not work. The mysql table is pretty simple. The group field in the MySQL is VARCHAR should i change this to TEXT? would that effect the display?

cocoonfx
01-03-2011, 12:54 PM
HELP? anyone??? completely stuck?

edbr
01-04-2011, 02:27 AM
its not the way i would approach it , so i kept quiet. (personally) i would use 1 select and a category and a condition to display the second set

jmichae3
01-04-2011, 09:03 AM
lost me there. the group by threw me off. didn't know why it was necessary. usually I like to see a sample of somebody's data before I give a recommendation about what to do with their database tables. unfortunately, this is not always convenient or possible (security?).

try your queries in phpmyadmin's SQL query box first. once you have perfected them to the point that they give you the data it's supposed to, THEN put those queries into your code. good rule of thumb to follow. also, you can use mysql's command-line sql tool if you have a local copy of the database on XAMPP. I usually make a batch file that feeds all the switches and parameters to sql.exe so I don't have to mess with long command-lines. then I can just type my sql commands.

and just out of curiosity, why do you have $_GET of 'image_group' and 'group'? did you mean to use the same thing? what are you using for a URL?

it looks to me like you already have your category.

by the way, group by can actually filter out things. so beware of group by.

jmichae3
01-04-2011, 09:32 AM
how about using ORDER BY instead of GROUP BY?
might be just what the database doctor ordered. tell me exactly what you are trying to do with your data.

are you simply wanting to group images by a category regardless of order, or is there an order to your categories/image_groups? just guessing that from the fact that you are using a VARCHAR for the group.

wait - I just saw some bogus code in your original.

change

$select_image_query = mysql_query("select * from images where image_page='Battleground'") or die(mysql_error());
$select_image2_query = mysql_query("select * from images GROUP By image_group") or die(mysql_error());




if($_GET['group'] == $select_image2_query['image_group']){
$populate_data_query = mysql_query("select * from images where image_group = '" . $_GET['image_group'] . "'");
$populate_data_array = mysql_fetch_array($populate_data_query);

}

to

$select_image_query = mysql_query("select * from images where image_page='Battleground'") or die(mysql_error());
$select_image2_query = mysql_query("select * from images GROUP By image_group") or die(mysql_error());
$row2=mysql_fetch_assoc($select_image2_query);
//is there more than one row? if so, do a while on this. like this:
//while ($row2=mysql_fetch_assoc($select_image2_query)) {
// //do whatever's appropriate for $row2 data fields
//}
mysql_free_result($select_image2_query); //always do this after you are finished with a query result

if($_GET['image_group'] == $row2['image_group']) {
$populate_data_query = mysql_query("select * from images where image_group = '" . $_GET['image_group'] . "'");
$populate_data_array = mysql_fetch_assoc($populate_data_query); //I don't know why there's not a while statement here.
}

cocoonfx
01-04-2011, 02:58 PM
cheers guys... I will have a play later and let you know how I get on.

cocoonfx
01-05-2011, 12:01 AM
Cracked it... I had to de construct jimi code and also my own and also looked at a tut DJ did.... can you tell I have not done PHP is a while.....


$select = mysql_query("select * from images") or die(mysql_error());
$row=mysql_fetch_assoc($select);

$select1 = mysql_query("select * from images GROUP By image_group") or die(mysql_error());
$nav=mysql_fetch_assoc($select1);

$upd = $_GET['upd'];
$update_id = $row['image_group'];

if ($upd){
$select2 = mysql_query("select * from images where image_page='Battleground' And image_group = '" . $_GET['upd'] . "'");
$row2 = mysql_fetch_assoc($select2);
}

///Then the navigation

<?php do {?>

<li><a href="test2.php?upd=<?php echo $nav['image_group'];?>"><span><?php echo $nav['image_group'];?></span></a></li>
<?php }while ($nav = mysql_fetch_array($select1)); ?>

// Then the images

<h2><?PHP echo $row2['image_page'];?> - <?PHP echo $row2['image_group'];?></h2>
<?php do {?>
<a href="images/<?php echo $row2['image_file_name'];?>" width="560" height="350" title="<?php echo $row2['image_title'];?>"/>
<img src="images/<?php echo $row2['image_file_name'];?>" width="150" height="150" alt="" />
</a>
<?php } while ($row2=mysql_fetch_array($select2));?><?PHP mysql_free_result($select2);?>

http://www.tamworthkickboxing.com/test2.php finally works...