Jump to content

[SOLVED] Form and PHP in the same file


Recommended Posts

php will never wait for input unless you are using it via the command line. isset() is simply a test to see if something 'is set':

 

<?php
  if(isset($var))
    echo '$var is set';
  else
    echo $var is not set';

  $var = 'foobar';

  if(isset($var))
    echo '$var is set';
  else
    echo $var is not set';
?>

$var is not set
$var is set

 

in the code i sent before, that if statement is checking to see if 'gid' is a valid array key inside the array $_GET. if it's not, that means gid is not in the url, and therefore no gallery has been selected

Link to comment
Share on other sites

Now can I make this test in between the <title> tags so that I can obtain the random number and display it in the window title bar ? Also I dont see why we are testing here. When the page is loading , obviously he doesnt immediately make a gallery selection , so can't we just randomly pick one gallery and display it to him?

Link to comment
Share on other sites

Also , one thing I don't understand is , how does the whole think work ?

 

Take a scenario.

 

When I choose to view gallery1 , the file gallery1.txt is opened , and the contents(image links) read off and images displayed.Now at exactly the same time , lets assume a guy in the US chooses the same gallery for viewing.Now since the file is already open , how does PHP re-open it once again ? Won't it result in an error ?

And what happens when people open two different galleries simultaneously ?

Link to comment
Share on other sites

If you want the Title to be reflective of the random gallery, just move the code that determines the gallery to the top of the page. Then, when you print the title, you will be access that information:

<?php
  //This is shorthand for if/else. Syntax:  (test) ? true : false
  //So if (isset($_GET['gid'])) is true, $gid will be set to $_GET['gid'], false it will do the rand()
  $gid = (isset($_GET['gid'])) ? $_GET['gid'] : rand(1,3);
?>
<html>
  <head>
    <title>Gallery <?php echo $gid;?></title>
  </head>
  <body>
<?php
  $file = "gallery{$gid}.txt"; //Update to use $gid
  echo '<table border="2" style="margin:auto;">';
  foreach(file($file) as $n => $line){
    echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';
    echo '<tr><td style="text-align:center;"><img src="'.$line.'"></td></tr>';
  }
  echo "</table>";
?>
  </body>
</html>

PHP starting to make sense yet?

 

Every time you load a page, it will spawn an independent PHP process. Each process will have it's own file handle when opening/reading a gallery file.

 

Multiple processes can read the same gallery file at the same time because it's just reading. Only when you get into writing a file do you have to worry about multiple people editing at the same time.

Link to comment
Share on other sites

Thanks for all the clarifications. I do recognise the C ternary operator.

 

I was progressing well with php , learning it on my own when suddenly this isset() confused me and made me think it was an event handler of some sort.Thats why started asking too many questions for a good C programmer. Plus you know the fact that the environment is different , it is running on a webserver and there are security issues to be thought of. Hence the profusion of questions.

 

Thanks for answering them patiently.

Link to comment
Share on other sites

  • 2 weeks later...

OK now , I have included javascript and tested it and found out that everything is working fine.

 

Now I want a rating system.I want people to rate my gallery.I have put radio buttons to do it. But here obviously , many people will be writing into the same file "rateN.txt" where N is the gallery number.How do I handle it ? And also I want to display the current rating for a gallery.I will have to read the list of rating numbers from the file and average them and display it.In other words the file will be read by me and written to by so many people simultaneously.How do I get it done ?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.