PDA

View Full Version : variables in php files


Zaiaku
24 Nov 2009, 23:01
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
24 Nov 2009, 23:33
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
24 Nov 2009, 23:59
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
25 Nov 2009, 00:29
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
25 Nov 2009, 00:44
$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
25 Nov 2009, 02:22
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
25 Nov 2009, 03:03
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
26 Nov 2009, 12:08
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
26 Nov 2009, 14:37
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
26 Nov 2009, 14:40
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
26 Nov 2009, 14:53
Unfortunately, he said my code didn't work. So, I don't know that making it global would fix whatever is wrong either.

Zaiaku
26 Nov 2009, 23:44
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
27 Nov 2009, 05:16
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
27 Nov 2009, 14:33
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.