Forum Moderators: coopster

Message Too Old, No Replies

Multiple Variables... with same name

script works fine with only one

         

coyote

5:46 am on Feb 20, 2004 (gmt 0)

10+ Year Member



I created a script for a rating system to be used on my website. I
include()
the rating script (rate.php) in the page and grab certain info from the variables on said page with
$_POST
.

The problem is with multiple items on one page. Items I would like to be rated (ex: $widget='291', $widget='292', etc) are displaying the rating and form for the first item. I've tried using

include()
inside the code for every item, instead of a blind include at the top, but the problem persists plus the page takes 10 million years to load.

I'm at a loss as to how I could fix this without giving each item a unique name and then adding tons of sql queries in rate.php.

webadept

7:07 am on Feb 20, 2004 (gmt 0)

10+ Year Member



If you are doing, what I think you are doing, I'm really surprised the page loads before your next birthday.

Every Include statement, loads a file, into the compiler, at compile time (which is when the page is called by the server to fullfill a browser request). So if you are including the same page, multiple times... eek! See the thread on spagettii coding.

The compilers for Script languages (though there is a bit of optimization) load all these pages into one page. So, you are not doing your program any good by mulitple loads.

I'll have to check about the variable names in include files, to see if they get different memory addresses than then variables in the main one. Something is scratching in the back of my head, which says they do (but I can't recall right now if that is Perl, or PHP which does this... might be Python). But the include discription you gave is way off base if that is really what you are doing.

Glenn Hefley

coyote

7:39 am on Feb 20, 2004 (gmt 0)

10+ Year Member



Every Include statement, loads a file, into the compiler, at compile time (which is when the page is called by the server to fullfill a browser request). So if you are including the same page, multiple times... eek! See the thread on spagettii coding.

I know all of that. I tried it as a last resort since the script was defaulting all "widget" values to the first's value.

jatar_k

4:35 pm on Feb 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you thought about appending an int to the end of the var varname?

It does make things a little more complex. Take a look at PHP Variable Variables [ca.php.net]

webadept

7:22 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



You sound frustrated coyote, and I didnt' mean to add to that.. it was just.. well.. never mind, what was happening on my end here should have part of your problem.. anyway..

It sounds like you need to do a re-write, with what you have already described. Trust me here, and you can ask any of the other experienced programmers on this list.. most of the folks in the spagetti thread .. this is the fastest, easiest way to fix that problem. Anything else you do, when your code is at this point, is going to cost you several times more in frustration, error chasing and hair loss. Probably a few pets as well.

Anyway.. take it easy, it's just code :-)

coyote

12:15 am on Feb 21, 2004 (gmt 0)

10+ Year Member



I've tried this:
$widget=$1; $$1='291';

and this:

$widget =& $1; $1='291';

Both result in an error message: "Expecting 'T_VARIABLE' or '$' on line (line it's on)"

?

I've been working on the script for a week and am to the point of scrapping it and moving on to other projects.

jatar_k

2:52 am on Feb 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



let's take a different look at it then.

You are passing the values to the rate script using post.

What does the post array look like foor a single item?
What does the post array look like foor multiple items?

Is it a problem with the construction of the array or with the reading of the array?

coyote

4:08 am on Feb 21, 2004 (gmt 0)

10+ Year Member



What does the post array look like foor a single item?

$_POST['widget']

What does the post array look like foor multiple items?

I didn't think creating a

$_POST
for mulitple items was needed with my script since I used "widget" as the variable for every item.
$widget =& $1; $1='291'
etc. Wouldn't POSTing "widget" get: "widget" -> "1" -> "291"?

Is it a problem with the construction of the array or with the reading of the array?

Most likely it's the new array. The script worked almost perfectly before, "almost" referring to the topic of this thread.
I can't figure out why the array is blowing the entire script, though.

jatar_k

4:59 pm on Feb 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it would seem the post vars are just overwriting the values. You end up with only 1.

I can't quite get a good understanding of how it is working though.

How do you post the multiple items to the page that includes the rate script? I think the rate script itself can just be changed to a foreach or some type of loop to fix it but you need to figure out how to give it access to the multiple values.

Could you possibly post your values to an array instead?
widget[] = "291";
widget[] = "292";

and then loop through the array? Again I am having a little trouble understanding the posting of the whole thing and where it is coming from and how it is posting to the page that includes the rate script.

coyote

6:26 am on Feb 24, 2004 (gmt 0)

10+ Year Member



Could you possibly post your values to an array instead?
widget[] = "291";
widget[] = "292";
and then loop through the array?

I tried that and the server timed out because it was taking too long to process the script.

I decided to give up on

$_POST
and use
$_GET
along with a dynamic URI query (ex: rate.php?widget=291). Everything works now. I just have to anchor link to the URI instead of being able to include the ratings and form in the page.

Thanks for trying to help.