Jump to content

why isnt this variable getting in to my query string?


jimmi8

Recommended Posts

HI,

 

I have this piece of code that set the query string depending on if a particular row is pulled from the database. The row in question is the one ive assigned to the variable upload. If it is set then one query string gets used and if it isnt another one gets used:

 

$sql = "SELECT entries.*, categories.*, uploads.* FROM entries INNER JOIN categories ON(categories.category_id = entries.category_id) LEFT JOIN uploads ON (uploads.blog_id = entries.blog_id) ORDER BY entries.date_submitted DESC LIMIT 3";
$query = mysql_query($sql);


while ($row = mysql_fetch_array($query)) {
  $title = $row['title'];
$body = substr($row['body'],0,400);
$author = $row['author_id'];
$date = $row['date_submitted'];
$cat = $row['category_id'];
$cat1 = $row['category'];
if(isset($row['upload_id'])) {
	$upload = $row['upload_id'];
}
else { $upload = NULL;}

  	$blog_id  = $row['blog_id'];



?>






<li><h3><?php echo $upload; ?><?php if(isset($upload)) { echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id;} else { echo '<a href="individualarticle.php?f=0&blog_id=' .$blog_id;} ?> "><?php echo $title; ?></a><?php if(isset($upload)) { echo ?><img src="pin.gif" /><?php ;}?></h3><p style="color: red; display: inline;">written by <?php echo $author; ?> on <?php echo $date; ?> Posted in<a href="categorypage.php?category_id=<?php echo $cat; ?>"><?php echo $cat1; ?></a></p><p><?php echo $body; ?></p></li>
<p><a href="individualarticle.php?blog_id=<?php echo $blog_id ?>">read on...</a></p>

 

Now it seems to work fine. If the row is set i can click through to the next page and everything works. The problem is that if the row is not set and the else is called and second query string gets used the $blog_id varaible doesnt get passed to the $_GET. When you click through the query string gets in to the url but theres no $_blog_id number on the end.

 

I cannot for the life of me work out why.

 

Could anyone spot the problem?

Link to comment
Share on other sites

In your linking part, try this:


<?php

echo '<li><h3>';
echo $upload;

if(isset($upload)){
  echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id. '">';
}
else{
  echo '<a href="individualarticle.php?f=0&blog_id=' .$blog_id. '">';
}

echo $title .'</a>';

if(isset($upload)){
  echo '<img src="pin.gif" />';
}

echo <<<_HTML
</h3>
<p style="color: red; display: inline;">written by {$author} on {$date} Posted in <a href="categorypage.php?category_id={$cat}">{$cat1}</a></p>
<p>{$body}</p>
</li>
<p><a href="individualarticle.php?blog_id={$blog_id}">read on...</a></p>
_HTML;

?>

Link to comment
Share on other sites

Your coding is a real mess... Try this:

 

<?php

$sql = "SELECT entries.*, categories.*, uploads.* FROM entries INNER JOIN categories ON(categories.category_id = entries.category_id) LEFT JOIN uploads ON (uploads.blog_id = entries.blog_id) ORDER BY entries.date_submitted DESC LIMIT 3";
$query = mysql_query($sql);


while ($row = mysql_fetch_array($query))
{
$title = $row['title'];
$body = substr($row['body'],0,400);
$author = $row['author_id'];
$date = $row['date_submitted'];
$cat = $row['category_id'];
$cat1 = $row['category'];
$upload = (isset($row['upload_id'])) ? $row['upload_id'] : "";
$blog_id  = $row['blog_id'];

echo "<li><h3>".$upload;
if(!empty($upload))
	echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id;
else
	echo '<a href="individualarticle.php?f=0&blog_id=' .$blog_id;
echo $title."</a>";
if(!empty($upload))
	echo "<img src=\"pin.gif\" />";
echo "</h3>";
echo "<p style=\"color: red; display: inline;\">written by ".$author." on ".$date.". "
echo "Posted in <a href=\"categorypage.php?category_id=".$cat."\">".$cat1."</a></p><p>".$body."</p></li>";
echo "<p><a href=\"individualarticle.php?blog_id=".$blog_id."\">read on...</a></p>";
}

?>

 

 

Orio.

Link to comment
Share on other sites

HI there,

 

Thanks for the reply. Unfortunatley that code did the same thing as, essentially, it worked in exactly the same way my more messy version did. The blog_id does not get passed to a an entry that doesnt have a file.

 

I am absolutley bewildered. I cannot work out why this is happening. Theres something intrinsically wrong with the way this script is working. Its something to do with the fact that im left joining on the uploads. Its beginning to get me down! 

Link to comment
Share on other sites

Hi there,

No its not that unfortunatley. When i echo out $blog_id in the if else:

 

if($upload == 0) {
                 echo $blog_id;
                 echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id .'">';
             } else {
                  echo $blog_id;
                 echo '<a href="individualarticle.php?f=0&blog_id=' .$blog_id .'">';
             }

 

it only gets through if there is an upload set. I think it has to do with my query. If i change the left join to inner join everythings working fine. I cant quite work out why and its a shame because i need a left join there!

Link to comment
Share on other sites

Ha solved it!

 

the query was too ambiguous, i added this: entries.blog_id

 

here:

 

SELECT entries.*, categories.*, uploads.*, entries.blog_id FROM entries INNER JOIN categories ON(categories.category_id = entries.category_id) LEFT JOIN uploads ON (uploads.blog_id = entries.blog_id) ORDER BY entries.date_submitted DESC LIMIT 3

 

thanks for your help  ;D

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.