Jump to content

Click to next image


JJohnsenDK

Recommended Posts

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");
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.