[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 07-05-2006, 05:49 AM   #1 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


PHP Problems

Hello,

On my site, it consists of a few PHP scripts. This is how it works:
Loads functions.php
Loads a php include, which contains a function that functions.php needs to process. However, within that function contains another variable, which another script needs to process (done afterwards).

The problem is, since the function requested the processing of functions.php, but the variable inside the function has not yet loaded, it does not work properly.

Ill try and make it more simple:
<?php include functions.php ?> (Function contains $id function processing)
<?php include content.php ?>

Content.php contains $id=$value. Function needs to process $id, but $value still has not been processed yet, therefore it does not work properly. Later in content.php it processes the value, but it has already asked functions to process the id, therefore the value is still unknown.

I've tried ob_start, but it doesnt work.

Any ideas? Thanks
nigelwong is offline  
Old 07-05-2006, 08:47 AM   #2 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
ob_start only stops output being sent to the browser.

If a function requires a variable and that variable is not available yet then you shouldn't be calling it.

Why are you calling it before you are ready?
Peter is offline  
Old 07-05-2006, 08:18 PM   #3 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


Thats why i am asking how do you delay calling it?

Actually i am not entirely sure if its the function that's not ready, or mysql playing tricks on me. If you set the variable down, function abc(x) , the mysql statment:
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id=x ")or die(" Error: ".mysql_error()); works fine.

However, if you put a variable in, (function abc($x); ), the mysql statement

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$x' ")or die(" Error: ".mysql_error());

doesnt work.

However later on in the script, you ask it to <?php echo $x ?>, $x echos.

Does anyone have any ideas?
nigelwong is offline  
Old 07-05-2006, 08:27 PM   #4 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
Uh.. function abc(x) wouldn't do anything and probably give you an error.

function abc($x) is correct and does not add quotes unless the quotes are in the variable and it should work.

I'm still not completely sure what you are doing and what you are trying to do..
Dan is offline  
Old 07-05-2006, 09:11 PM   #5 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


Well here is whats happening:

If i have
<?php abc('1') ?> run through the CMS it works

if i have
<?php abc('$x') ?> run through the CMS, it Doesnt Work

it has a $output = str_replace("$x", "news_arr[0]", "$output");
in the script which replaces the $x, but the function has already been called, and it is sending $x without the processed variable.

Therefore, i need a way to delay the function, even when it is called.

Okay this is the script:

Functions.php:
PHP Code:
<?php
function rating_bar($id) {
// this is to get the current rating(s) and display it visually

//Connect to Database
    
$dbhost = 'localhost';
    
$dbuser = 'xxxx';
    
$dbpass = 'xxxxx';
    
$dbname = 'xxxxx';
    
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die  ('Error connecting to mysql');
    
mysql_select_db($dbname);
    
$tableName="ratings";

//id of the thing you want to rate

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$id' ")or die(" Error: ".mysql_error());
while (
$numbers=mysql_fetch_assoc($query));
$count2=$numbers["total_votes"];//how many votes total
$current_rating=$numbers['total_value'];//total number of rating added together and stored
$tense=($count2==1) ? "vote" : "votes";//plural form votes/vote
$test=1;

$ip = $_SERVER['REMOTE_ADDR'];

$result=mysql_query("SELECT count(*) FROM $tableName WHERE used_ips LIKE '%".$ip."%' AND id='$id' "); //Pattern match ip:suggested by Bramus! [url]http://www.bram.us/[/url] - this variable searches through the previous ip address that have voted and returns true or false
$voted = mysql_result($result, 0, 0);

mysql_close($conn);

// draw the rating bars
// still to do: need to not allow them to vote if they've already been here and voted

?>

        <div id="unit_long<?php echo $id ?>">
        <ul class="unit-rating"">
        <li class='current-rating' style="width:<?php echo $count2 ?>px;">Currently <?php echo @number_format($current_rating/$count2,2); ?>/5</li>
            <?php
            
for ($ncount = 1; $ncount <= 5; $ncount++) { ?>
                <li><a href="#" title="<?php echo $ncount ?> out of 5" class="r<?php echo $ncount ?>-unit" onClick="javascript:sndReq('<?php echo $ncount ?>','<?php echo $id ?>','<?php echo $ip ?>')"><?php echo $ncount ?></a></li>
        <?    }
            
$ncount=0; // resets the count
            
?>
        </ul>
        </div>
<?
}
?>
Article:
PHP Code:
<?php rating_bar('{news-id}'); ?>
Part of the Output File:
PHP Code:
$output = str_replace("{news-id}", $news_arr[0], $output);

Last edited by nigelwong; 07-05-2006 at 09:27 PM.
nigelwong is offline  
Old 07-06-2006, 02:10 AM   #6 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


Update: Somebody told me i have to use the $globals command, but how would i do it?
nigelwong is offline  
Old 07-06-2006, 03:17 AM   #7 (permalink)
tm
Senior Member
 
tm's Avatar
 
Join Date: Nov 2005
Location: on a oil rig just off Ireland
Posts: 1,409
374.65 NP$ (Donate)

tm is a glorious beacon of lighttm is a glorious beacon of lighttm is a glorious beacon of lighttm is a glorious beacon of lighttm is a glorious beacon of light


<?php abc('$x') ?> won't work. Try <?php abc("$x") ?> or <?php abc($x) ?>.
__________________
You design in photoshop, I code into valid XHTML/CSS.
Professional PSD, PNG or HTML to tableless XHTML/CSS designs.
For more info, send me a PM.
tm is offline  
Old 07-06-2006, 03:46 AM   #8 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
You don't put quotes when you use a variable inside a function.

abc($x)
Dan is offline  
Old 07-06-2006, 06:44 AM   #9 (permalink)
NamePros Member
 
Join Date: Mar 2006
Location: USA - RI
Posts: 49
216.70 NP$ (Donate)

Horranus is an unknown quantity at this point


Quote:
Originally Posted by nigelwong
Update: Somebody told me i have to use the $globals command, but how would i do it?

A global command tells the function that the variable you set inside the function should be available to the rest of the script.

PHP Code:

function yada()
{
  global
$abc
  $abc
= 123
}

echo
$abc
The above code would allow you to use $abc outside of the function;
The below code would not

PHP Code:

function yada()
{
$abc = 123
}

echo
$abc
The top set of code would output 123, the bottom would give you an error because you did not tell the function to make the variable accessible to the rest of the script and / or other functions.

Hope that helps.
Horranus is offline  
Old 07-06-2006, 07:41 PM   #10 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


Am i supposed to put the function script inside the same php file as my output script? Cause currently they are both php included on the page.

I dont think the globals command will help. Here is a more detailed rundown of whats happening:


The functions script is php included onto my page (script posted in previous post)

A template for my CMS is stored like this:
<div>
{title}
{story}
<?php ranking('{news-id}')?> (ive removed the ' ' but i get Parse error: syntax error, unexpected '{', expecting ')' )
</div>

In the output script, it loads news.txt file, then the template.
It looks in the template for {title}, {news-id} etc. then does a series of $output = str_replace("{news-id}", "news_arr[0]", "$output");
then echo's $output.

Hopefully what i wanted it to echo the output like:
<div>
Title Goes here
Story Goes here
<?php ranking('0001')?>
</div>

Last edited by nigelwong; 07-06-2006 at 07:55 PM.
nigelwong is offline  
Old 07-06-2006, 07:44 PM   #11 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
As long as you include the function, you can run it.

You don't need to do anything with global.
Dan is offline  
Old 07-06-2006, 08:36 PM   #12 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


Ok im making abit of process.

I have this:
<?php rating_bar($newsid) ?>

And my function:
PHP Code:
<?php
function rating_bar($id) {

// this is to get the current rating(s) and display it visually

//Connect to Database
    
$dbhost = 'localhost';
    
$dbuser = 'xx';
    
$dbpass = 'x';
    
$dbname = 'xx';
    
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die  ('Error connecting to mysql');
    
mysql_select_db($dbname);
    
$tableName="ratings";

//id of the thing you want to rate

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$id' ")or die(" Error: ".mysql_error());
while (
$numbers=mysql_fetch_assoc($query));
$count=$numbers["total_votes"];//how many votes total
$current_rating=$numbers['total_value'];//total number of rating added together and stored
$tense=($count2==1) ? "vote" : "votes";//plural form votes/vote
$test=1;

$ip = $_SERVER['REMOTE_ADDR'];

$result=mysql_query("SELECT count(*) FROM $tableName WHERE used_ips LIKE '%".$ip."%' AND id='$id' "); //Pattern match ip:suggested by Bramus! http://www.bram.us/ - this variable searches through the previous ip address that have voted and returns true or false
$voted = mysql_result($result, 0, 0);

mysql_close($conn);

// draw the rating bars
// still to do: need to not allow them to vote if they've already been here and voted


?>

        <div id="unit_long<?php echo $id ?>">
        <ul class="unit-rating"">
        <li class='current-rating' style="width:<?php echo @number_format($current_rating/$count,2)*25; ?>px;">Currently <?php echo @number_format($current_rating/$count,2); ?>/5</li>
            <?php
            
for ($ncount = 1; $ncount <= 5; $ncount++) { ?>
                <li><a href="#" title="<?php echo $ncount ?> out of 5" class="r<?php echo $ncount ?>-unit" onClick="javascript:sndReq('<?php echo $ncount ?>','<?php echo $id ?>','<?php echo $ip ?>')"><?php echo $ncount ?></a></li>
        <?    }
            
$ncount=0; // resets the count
            
?>
        </ul>
        </div>
<?
}
?>
However, the only line that the script can process properly is:
<div id="unit_long<?php echo $id ?>">

This line won't work:
<?php echo @number_format($current_rating/$count,2)*25; ?>

If i have something like $count
<div id="unit_long<?php echo $count ?>">

it outputs nothing.

Last edited by nigelwong; 07-06-2006 at 08:54 PM.
nigelwong is offline  
Old 07-06-2006, 08:40 PM   #13 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
<?php ratings($newsid) ?>..

There is no function called ratings.. it is rating_bar

<?php rating_bar($newsid); ?>
Dan is offline  
Old 07-06-2006, 08:54 PM   #14 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


Sorry that was a typo.

But if i had something like:
<?php
function rating_bar($id2) {
echo $id2
}
?>

It would work

However, with all the sql stuff, it doesn't work ($count etc.).
nigelwong is offline  
Old 07-06-2006, 09:01 PM   #15 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
So that means you coded the function wrong..

Doesn't have anything to do with calling it.
Dan is offline  
Old 07-06-2006, 09:11 PM   #16 (permalink)
NamePros Member
 
Join Date: Nov 2005
Posts: 66
64.00 NP$ (Donate)

nigelwong is an unknown quantity at this point


It was coded right, i typed it in my post wrong.

I now found out its my SQL Statement:

This works:
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id=1152076887")

This doesnt.
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$id'")

Even though if you echo $id in the function, it gives you 1152076887
nigelwong is offline  
Old 07-06-2006, 09:34 PM   #17 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id=$id")

You don't put quotes around a number. ($id)
Dan is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 05:25 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85