JJohnsenDK Posted January 7, 2008 Share Posted January 7, 2008 Hey Im trying to figur out the best way to get the next image from a mysql image database. You know you have one image and then you can click a button to get the next. Very easy if my image had 1,2,3,4,5 ids in the database, but they dont. they have random numbers like 2, 9, 33, 83 and so on. Here is my code, but im stock atm. Im trying something with arrays, but it doesnt seem to be the solution: <script type="text/javascript"> // JavaScript Document var xmlHttp; var url; var browser = navigator.appName; if (browser == "Microsoft Internet Explorer"){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else { xmlHttp = new XMLHttpRequest(); } if(xmlHttp == null) { alert("Din browser understøtter ikke AJAX. Siden kan derfor ikke vises."); } function showLink(image){ var url = "info.php?image="+image; xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange=stateChangedShowLink; xmlHttp.send(null); } function stateChangedShowLink(){ if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("show_menu_links").innerHTML=xmlHttp.responseText; } } </script> <?php $image = trim(strip_tags($_GET['image'])); $id = trim(strip_tags($_GET['id'])); if(isset($site)){ $site = get_site_name($id); $text = get_text(1, $site, 1); if(!empty($text)){ ?> <div id="kirke_content"> <div id="kirke_image"> <div> <?php $array = array(); $imageQuery = dbquery("SELECT site_image_id FROM ".DB_PREFIX."site_image WHERE site_text_id = '$id'"); while($imageRow = dbarray($imageQuery)){ $array[] = $imageRow['site_image_id']; } if(in_array(1, $array)){ $firstImage = $array[0]; echo $firstImage; } $query = mysql_query("SELECT url, name FROM ".DB_PREFIX."site_image WHERE site_image_id = '$firstImage'") or die(mysql_error()); $row = mysql_fetch_array($query); echo "<div><img src='".$row['url']."' alt='".$row['name']."' style='width: 270px; border-bottom: 1px solid black;' /></div>"; ?> </div> <div class="kirke_image_panel"> <?php ?> <a href="" onclick="showImage('')"> < </a> | <a href="" onclick=""> > </a> </div> </div> <div id="kirke_text"><?=$text;?></div> <div style="clear: right;"></div> </div> <?php }else{ redirect("home.php?site=error"); } }else{ redirect("home.php?site=error"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84929-click-to-next-image/ Share on other sites More sharing options...
Ken2k7 Posted January 7, 2008 Share Posted January 7, 2008 Where's the ID show_menu_links? Quote Link to comment https://forums.phpfreaks.com/topic/84929-click-to-next-image/#findComment-433027 Share on other sites More sharing options...
awpti Posted January 7, 2008 Share Posted January 7, 2008 Bit of a hack using SQL.. SELECT ..... WHERE image_id >= $current_image_id LIMIT 2 ORDER BY image_id ASC That'll get the current ID + next one. No the prettiest way to do it. Easiest though. Quote Link to comment https://forums.phpfreaks.com/topic/84929-click-to-next-image/#findComment-433031 Share on other sites More sharing options...
JJohnsenDK Posted January 7, 2008 Author Share Posted January 7, 2008 allright... awpti... i will try your suggention, if my own doesnt work... found the array_seach as a handy tool, it will hopefully work, without sql hacks , but stay turned cause this isnt solved yet Quote Link to comment https://forums.phpfreaks.com/topic/84929-click-to-next-image/#findComment-433047 Share on other sites More sharing options...
GingerRobot Posted January 7, 2008 Share Posted January 7, 2008 In what way is it not pretty? As far as i can see, it's the only way to do it. Personally i'd also add in some error checking to return the first image if the current image is the last: <?php $sql = mysql_query("SELECT * FROM tbl WHERE image_id > $current_id ORDER BY image_id LIMIT 1") or die(mysql_error()); //not sure why you had a limit 2 previously? if(mysql_num_rows($sql) == 0){ $sql = mysql_query("SELECT * FROM tbl ORDER BY image_id LIMIT 1") or die(mysql_error()); } $row=mysql_fetch_assoc($sql); ?> This does, of course, rely on you passing the current id through the url. Quote Link to comment https://forums.phpfreaks.com/topic/84929-click-to-next-image/#findComment-433066 Share on other sites More sharing options...
JJohnsenDK Posted January 7, 2008 Author Share Posted January 7, 2008 thanks gingerrobot... will have a go with this... Quote Link to comment https://forums.phpfreaks.com/topic/84929-click-to-next-image/#findComment-433077 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.