View Full Version : variables in php files
Zaiaku
11-24-2009, 10:01 PM
I'm having a slight issue with a script I'm making. And I'm wondering if something very simple I'm missing. Right now the code is hard coded into the php file, and so is the settings. The problem is with a vairable. It's pulling a list of the last X members from the database.
The problem is that when using a variable ex: $grabbedmembers
It doesn't work in the SQL call. Variable is empty even though near the top of the php file I have $grabbedmembers = 5
But if I just replace the variable with the number in the SQL call it works.
Do all variables in php file need to be register before working? Or just variables that will be used in templates?
Lynne
11-24-2009, 10:33 PM
Post your code and we'd be able to help better. Variables need to be registered for use in templates not in the php files.
Zaiaku
11-24-2009, 10:59 PM
Here;s the one for the last few threads:
$Zforum = 5;
private function fetch_lastids()
{
$lastx = $this->registry->db->query_first("
SELECT COUNT(`threadid`) AS `lastx`
FROM " . TABLE_PREFIX . "thread AS thread
WHERE thread.forumid IN (0,". $Zforum. ")
AND thread.visible = 1
");
$this->threadscount = $lastx['lastx'];
}
Now where it says $Zforum in the SQL quiery. If I just replace that with the number 5 then it works.
Lynne
11-24-2009, 11:29 PM
Have you tried passing the variable to the function.
private function fetch_lastids($a_var)
{
$lastx = $this->registry->db->query_first("
SELECT COUNT(`threadid`) AS `lastx`
FROM " . TABLE_PREFIX . "thread AS thread
WHERE thread.forumid IN (0,". $a_var . ")
AND thread.visible = 1
");
$this->threadscount = $lastx['lastx'];
}
..... fetch_lastids($Zforum)....
Zaiaku
11-24-2009, 11:44 PM
$a_var remains empty. For some reason I can't get it to store any value what so ever. Although works fine in vb3.8. Thre must be something new in vb4 causing the problem
winstone
11-25-2009, 01:22 AM
just before your mysql query, put:
var_dump($a_var);
exit;
if result was null, put the code above just after the code "$Zforum = 5;"
if the result was correct, some code in between is messing up with this variable
Zaiaku
11-25-2009, 02:03 AM
just before your mysql query, put:
var_dump($a_var);
exit;
if result was null, put the code above just after the code "$Zforum = 5;"
if the result was correct, some code in between is messing up with this variable
OK it did return the right number so now I know its something in the coding that is clearing all stored variables. At least now I'm cutting down the problem.
OK I figured it out. Wow programming in vb4 did add something new to the variables. I figured it out. It was just a delaration inside the funtion. Once added it worked flawlessly. Thx everyone for helping.
ragtek
11-26-2009, 11:08 AM
In your first code, $Zforum was not in the scope!
You can't use variables inside a function which are declared outsite withouth an global ;)
$Zforum = 5;
private function fetch_lastids()
{
global $Tforum;
$lastx = $this->registry->db->query_first("
SELECT COUNT(`threadid`) AS `lastx`
FROM " . TABLE_PREFIX . "thread AS thread
WHERE thread.forumid IN (0,". $Zforum. ")
AND thread.visible = 1
");
$this->threadscount = $lastx['lastx'];
}
Lynne
11-26-2009, 01:37 PM
In your first code, $Zforum was not in the scope!
You can't use variables inside a function which are declared outsite withouth an global ;)
I thought passing the variable would take care of that (see my post 4). :confused:
ragtek
11-26-2009, 01:40 PM
I thought passing the variable would take care of that (see my post 4). :confused:
Yea, you're right, my post was referring to his code^^
Youre code will also work, i showed him just another possible code.
Lynne
11-26-2009, 01:53 PM
Unfortunately, he said my code didn't work. So, I don't know that making it global would fix whatever is wrong either.
Zaiaku
11-26-2009, 10:44 PM
Ragtek is right. Doing a global declaration make it work perfectly. That was all that way needed. The code was based off of his newportal he made on vbgermany. After learning by his coding I was able to make a few mods from it. Basically a catergory listening for different forums. I wanted it hard coded becuase all I wanted to do was uploading the file and don't have to ever worry about it also now have even possible plugins that may interfere with other mods.
--------------- Added 1259286815 at 1259286815 ---------------
How do I changed the query to pull threads from all forums?
ragtek
11-27-2009, 04:16 AM
do you want all threads from all forums?
select * from thread
And my newsscript (do you mean vB4 version?) is very strange coded;)
I wanted to do everything with oop^^
Without oop it's much easier;)
--------------- Added 1259302740 at 1259302740 ---------------
Also this would work:
$this->registry->db->query("select... ");
because i'm sending the $vbulletin object to my news constructor.
$news = new ragteknews($vbulletin, true);
Zaiaku
11-27-2009, 01:33 PM
And my newsscript (do you mean vB4 version?) is very strange coded;)
Yeah you not kidding. That's a huge understatement too. :D
I learn alot from it though and was able to clean up some of the and add a few things.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.