在MySQL查询中使用具有更改值的变量?

时间:2011-11-13 04:36:30

标签: php mysql url variables

我正在为我正在开发的网站构建标记系统。我有一个变量用于图像的网址,当用户选择不同的图片时,该变量会发生变化。我正在使用MySQL命令为所选图片显示3个标签,但如果我给它更改变量它就不起作用。但是,如果我手动插入图像的网址,则标题显示。但是,无论您使用的是什么图片,这些都会显示出来,所以这并没有真正帮助。如果我在一个变量下存储一个url,那也可以,但不能随时更改。

Here是网站。

我知道这不是很容易理解,所以如果您希望我尝试重新解释某些内容或者您是否希望我包含一些代码,请告诉我。谢谢!

编辑:好的,我会在这里重试。首先,这是代码:

<?php 
   $username = $_GET['username']; 
   session_start(); 
   $_SESSION['username'] = $username; 

   //Database Information 
   $dbhost = ""; 
   $dbname = ""; 
   $dbuser = ""; 
   $dbpass = ""; 

   //Connect to database 
   mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); 
   mysql_select_db($dbname) or die(mysql_error()); 

   //Do the query 
   $query = mysql_query("SELECT * FROM images ORDER BY idnum DESC LIMIT 5"); 

   // Generate an array of all images.

   $images = array(); 
   while($image = mysql_fetch_array($query)) { 

   // Adds each image to the images array. 

   $images[] = $image; 
} 

$image['filename'] = $current_image;

    $query_captions = mysql_query("SELECT * from captions WHERE image = '$current_image' ORDER BY idnum DESC LIMIT 3");
    $captions = array(); 
    while($caption = mysql_fetch_array($query_captions)) { 
    $captions[] = $caption; 
    }

?> 

<?php 

   // Beginning attempt at a caption script. 



?> 

<html> 
  <head> 
  <title>Home - Site in Development</title> 
  <link rel="stylesheet" type="text/css" href="styles.css"/> 
  <script type="text/javascript"> 

   // Switches the url to view large image. 

   function switchImageUrl(url, width, height) { 
     var x = document.getElementById('caption_selection'); 
     var y = document.getElementById('caption_input');
     var z = document.getElementById('input_value');
        x.style.display = 'none';  
        y.style.display = 'none';
        z.value = '';

   document.getElementById('center_frame').style.backgroundImage = 'url' + '(' + url + ')'; 
     document.getElementById('center_frame').style.width = width; 
     document.getElementById('center_frame').style.height = height; 
   } 

  </script> 


      <!--Toggles view of caption selection tools and resets after.-->

    <script type="text/javascript"> 

   function selectionToggle() { 
    var x = document.getElementById('caption_selection'); 
    var y = document.getElementById('caption_input');
    var z = document.getElementById('input_value');
    if(x.style.display == 'block') 
      x.style.display = 'none'; 
    else 
      x.style.display = 'block'; 
         x.style.width = '75px'; 
         x.style.height = '75px'; 
         x.style.top = '0px'; 
         x.style.left = '0px'; 

    if(y.style.display == 'block') 
      y.style.display = 'none'; 
    else 
      y.style.display = 'block'; 
  z.value = '';
   } 


    </script> 

    <script type="text/javascript">

     function collectAttributes() {
      var cap = document.getElementById('caption_selection');
      var cw = document.getElementById('cap_width');
      var ch = document.getElementById('cap_height');
      var cl = document.getElementById('cap_left');
      var ct = document.getElementById('cap_top');
      cw.value = cap.style.width;
      ch.value = cap.style.height;
      cl.value = cap.style.left;
      ct.value = cap.style.top;
    }

    </script>


    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
     <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 
     <script> 
     $(document).ready(function() { 
        $("#caption_selection").draggable({ 
        containment : "#center_frame" 
        }); 
        $("#caption_selection").resizable({ 
        containment : "#center_frame" 
        }); 
     }); 
     </script> 

  </head> 
  <body onLoad="CalculateAllImageWidthes()"> 
  <div id='account_links'> 
   <?php 
   if ($_SESSION['username']) { 
   echo "Welcome $username!"; 
   } else { ?> 
   <a href='login.php'>Login</a> | <a href='register.php'>Register</a> 
   <?php } ?> 
  </div> 

  <h1>Picture Captions</h1> 
  <br/> 
  <br/> 
  <div id="left_bar"> 
   Submit a picture <a href="upload.php">here</a>. 
  <hr/> 
  <h2>Top Images</h2> 
  <br/> 

  <div id="front_pg_images"> 
   <?php foreach($images as $image) { ?> 
   <a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" width="72px" height="58px" id="front_pg_thumbnail"/></a>
   <?php echo $image['name']." - by ".$image['submitter']; ?> <br/> 
   <br/> 
   <?php } ?> 
  </div> 

  </div> 
  <div id="center_frame" style="width: <?php echo $image['width']; echo "px" ?>; height: <?php echo $image['height']; echo "px" ?>; background-image: url('<?php echo $image['filename'];?>')" > 
  <?php foreach($captions as $caption) { ?> 
  <div id="set_caption" style="width:<?php echo $caption['width'].'px';?>;height:<?php echo $caption['height'].'px';?>; left:<?php echo $caption['posleft'].'px';?>; top:<?php echo $caption['postop'].'px';?>"><?php echo $caption['text'];?></div>
  <?php } ?> 
  <div id="caption_selection" style="display:none"> 

  </div> 

  </div> 
  <div id="toggle_select_container"> 
    <input type="button" id="toggle_select" onClick="selectionToggle()" value="Caption"/> 
    <div id="caption_input" style="display:none">
    <form action="postcaption.php" method="post" onSubmit="collectAttributes()">
   <br/>
   <textarea name="caption_text" cols="27" rows="4" id="input_value"></textarea><br/>
   <input type="hidden" name="filename" value="<?php echo $image['filename'];?>"/>
   <input type="hidden" name="width" value="" id="cap_width"/>
   <input type="hidden" name="height" value="" id="cap_height"/>
   <input type="hidden" name="left" value="" id="cap_left"/>
   <input type="hidden" name="top" value="" id="cap_top"/>
   <input type="submit" value="Post Caption"/>
    </form>
    </div>
  </div> 


  </body> 
</html> 

当用户点击缩略图时,它使用JavaScript函数更改图像。记下foreach。对于页面上的其他任何地方,$image['filename']已经完美地用于类似的用途,但是当我尝试使用它时,它只是不起作用。

0 个答案:

没有答案