Forum Moderators: coopster

Message Too Old, No Replies

PHP deferred value

php, mysql

         

mrfori

11:41 pm on Sep 23, 2003 (gmt 0)

10+ Year Member



Hi guys,

I need help with PHP. I have a couple of questions.

1. Is there a way of deferring a value of a variable? I mean I want to print a variable on the top of the page but I only assign its value on the bottom.

2. And, I want to insert data (lots of data) on a one column mysql table where the field a is primary key. I dont want any duplicates in my table and that is why I set the field as a primary key. (I want mysql to check for duplicates for me). But when I try to insert a duplicate data the execution is aborted and I get an error message. I would like my script to run smoothly and silent.

Do you have an idea how to solve this problem?
( I dont have access to php.ini or any configuration files)

thanks a lot

coopster

1:08 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



1. Is there a way of deferring a value of a variable? I mean I want to print a variable on the top of the page but I only assign its value on the bottom.

Print the variable, then process...? I'm not sure if I'm missing what you are trying to accomplish here, but it would be something like this:

<html><head><title>Title</title><head><body>
<h1>This is my <?php print $variable;?><h1>
<?php
// do processing here...
$variable = $new_value;
?>
</body></html>

2. And, I want to insert data (lots of data) on a one column mysql table where the field a is primary key. I dont want any duplicates in my table and that is why I set the field as a primary key. (I want mysql to check for duplicates for me). But when I try to insert a duplicate data the execution is aborted and I get an error message. I would like my script to run smoothly and silent.

I usually perform a SELECT statement first, and if any rows are returned, I know I have a duplicate and handle the error message accordingly, showing the user the duplicate information they are attempting to INSERT.

Tapolyai

1:18 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe the PHP interpreter would have evaluated the variable prior to assigning your "second" value.

That is, PHP translates the code in sequential order, and does not go back to "re-evaluate it" if the value changes. That would be a very bad thing in almost all cases.

An alternative solution to this is to use an immediate single refresh once the page is complete.

Example pseudo-code:


- check refresh flag
--- if not refreshed - refresh in 0 secs once page loads, and set refreshed flag
- display $value (undefined)
- do something
- generate new content for $value

Theoretically this should work.

mrfori

3:46 pm on Sep 24, 2003 (gmt 0)

10+ Year Member



Thanks four posts guys

1. I understand that doing a Select first will solve the problem but will affect performance a bit .. Is there a better way of doing it? Or even with a select which way is the best, like would you do one $db->getAll() and put data on an array or do a $db->query(select) prior every insert?

2. When u say "refresh" you mean <META HTTP-EQUIV="Refresh" CONTENT="0;URL=url.php">? Would that try to insert the same data again? ( i mean, would it call the page with the same query string or with no query string ) ..

thanks again : )

coopster

4:03 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



1) You could just attempt an INSERT and do error handling [us2.php.net], but I've found that by performing a select first, you can actually retrieve the duplicate row and show it to the user as part of the error-response message to them. Not only are they now aware they have an error (duplicate), but you have also reminded/showed them exactly what that is.

2) Are you trying to create a "data-holder" at the top-part of outputted html, and later in the script actually modify the variable that was output and you want to update the "data-holder" with the *new* value?

ergophobe

4:19 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I mean I want to print a variable on the top of the page but I only assign its value on the bottom.

Could you be more specific? How do you assign it's value at the bottom and why? I must be missing something, but normally I do all processing and then plug values into a template. In other words, other than variable substitution and a few essentials, there usually is no processing after the first print statement.