Forum Moderators: coopster
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
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.
<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.
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.
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 : )
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?
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.