Jump to content

Recommended Posts

Okay, I need some help here because I'm just not getting it.

 

This is a shoutbox script I found elsewhere and I added to it (or tried anyhow) to suit my needs. It was originally set up with only 4 fields, ID, Date, User, Message. The shoutbox was global.

 

What I am attempting to do is have it less global, I.e., have it used as a comment box on each different recipe on my site, by using $recipeID in both the read and write functions for the shoutbox, so, for example, if recipeID=1, it only shows the shouts that were made for that recipeID.

 

Problem is, no matter what I do, I can't get it to add the "recipeID" value to the database.

 

I created a new field and called it ShoutBoxID. Then I tried having it add the recipeID number to the database in that field, and for when messages are displayed, have it search only for messages that have that ID in it. Something I'd consider pretty straight forward.

 

I define $recipeID on my page recipes.php, where I include shoutbox.php. I even made sure, after my last mistake, to include global $recipeID; inside the function. No good.

 

Here's the code for the two problem areas:

 

function getContent($link, $num){
global $prefix, $recipeID;

$res = @mysql_query("SELECT ShoutBoxID, date, user, message FROM ".$prefix."shoutbox where ShoutBoxID='".recipeID."' ORDER BY date DESC LIMIT ".$num, $link);
if(!$res)
	die("Error: ".mysql_error());
else
	return $res;
}
function insertMessage($user, $message){
global $prefix, $recipeID;
$query = sprintf("INSERT INTO ".$prefix."shoutbox(ShoutBoxID, user, message) VALUES('$recipeID', '%s', '%s');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));
$res = @mysql_query($query);
if(!$res)
	die("Error: ".mysql_error());
else
	return $res;
}

 

I've tried echoing recipeID every way I know how, $recipeID, '$recipeID', ".$recipeID." and even '".$recipeID."' and nothing has worked.  Am I simply missing something or what?

 

Oh, yeah, everything (except the ShoutBoxID (aka $recipeID)) is being stored correctly and I'm not getting an error, but the field ShoutBoxID is always empty.

What I am attempting to do is have it less global,

 

Using the 'global' keyword inside the function actually does the opposite. It makes the function 'hard-wired' to use whatever value is in that variable and you cannot use more than one instance of that function on your page without needing to remember to setup that variable (adds extra code and extra work for the programmer to remember to do this for each function call) before each function call. You should only pass values into functions using function parameters (like you are already doing with the $link and $num values.)

 

Can you imagine what php would be like as a programming language if it brought values into all its' built-in functions using the global keyword. They would all need to be uniquely named so that you could use more than one function in a row without them conflicting with each other and it would make for a programming nightmare because the programmers would need to remember or lookup ALL the names of the variables they needed to set before calling a function.

 

As to your current problem, you have not shown how you are setting $recipeID or including shoutbox.php, so it is simply impossible for anyone in a forum to help with why $recipeID does not have a value when your function is called.

Okay, I wasn't sure if you needed that or not.

 

Here are the important parts of my recipes.php page:

 

<?php
session_start();
include("includes/db.php");
include("includes/functions.php");
$username=$_SESSION['username'];
$recipeID=sanitize($_GET['recipeID']);

~~ irrelevant page coding ~~

Comments:<br><br>
<form method="post" id="form">  
<input class="text user" id="nick" type="hidden" value="<?php echo $username;?>" />
         <table>  
<?php 
if ($_SESSION['username'])
{
?>             <tr>  
                 <td><label>Your Comment</label></td>  
                 <td><input class="text" id="message" type="text" MAXLENGTH="255" /></td>  
               </tr> 
		   <tr>  
                 <td></td>  
                 <td><input id="send" type="submit" value="Shout it!" /></td>  
             </tr> 
<?php
}
ELSE 
{
echo "<tr><td colspan='2' align='center'>You must be logged in to leave comments</td></tr>";
}
include ("includes/shoutbox.php");
?> 
              
         </table>  
     </form>  
     <div id="shoutcontainer">  
         <ul class="menu">  
             <li>Comments</li>  
         </ul>  
         <span class="clear"></span>  
         <div class="content">  
             <h3>Latest Messages</h3>  
             <div id="loading"><img src="css/images/loading.gif" alt="Loading..." /></div>  
             <ul>  
             <ul>  
         </div>  
     </div>  
     <script type="text/javascript" src="includes/jquery.js"></script>  
     <script type="text/javascript" src="includes/shoutbox.js"></script>  

Have you determined that $recipeID has a value at all at any point your code?

 

To pin point the location of the problem in your code, you must determine at what point $recipeID has an expected value and at what point it does not.

Yes, in shoutbox.php I have echoed $recipeID and it has returned the correct value:

 

<?php
include("db.php");
define("HOST", "FOO");
define("USER", "FOO");
define("PASSWORD", "FOO");
define("DB", "FOO");
echo "ID: ".$recipeID."";
/************************/
FUNCTIONS
/************************/
function connect($db, $user, $password){
$link = @mysql_connect($db, $user, $password);

 

So I know it is reaching the page, it's simply not being accepted (or, complicatedly not being accepted :P) in the SQL queries inside the functions themselves.

 

The funny thing is, when I echo it here, it shows above the shoutbox as ID=1, but inside the shoutbox there is another version of it that states: ID= , no value behind it.

 

Keep going. You need to pin down where in your code, right up to the point where the function(s) are called, that it no longer has the value. Your specific code between the point where it does have a value and where it does not is where the problem is.

I'd like to add something could make you think in a whole new way both your functions

 

function getContent($link, $num)

function insertMessage($user, $message)

 

have globals but why I figure when you can just add them as parameters?

 

function getContent($link, $num, $prefix, $recipeID)

function insertMessage($user, $message, $prefix, $recipeID)

 

problem solved?

if you think I did a bad job then you are mistaken I'm not the one passing

$link in getContent when you can make that global too.

 

for some reason in insertMessage $link is global and in getContent it's not global?

 

you have to make up your mind :P seriously :)

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.