Jump to content

Pass variable in http address to php


schris403

Recommended Posts

I'm trying to write a fairly simple php script which looks at a csv text file in the same directory for its data based on a variable that is passed in the address but am really struggling on this.. An example of what the address could be is:

http://www.something.com/script.php?field1=1234

 

The database.txt file will contain something like this:

 

1234,yes,yes,yes

4567,no,yes,yes

8901,yes,no,no

 

 

When field1 is 1234, it should then look at the next three fields to determine what result to display. For example,

yes,yes,yes should give a result of "I work"

no,yes,yes should result "I am disabled"

yes,no,no should be "I am done"

 

 

Any help or guidance would be greatly appreciated, I've been struggling with this for weeks and can't even partly get it to work the way I want..

 

Thanks so much!

 

Chris

Link to comment
Share on other sites

here you go I could make it a little better if you wish but here is the initial code

 

$_GET['field1'] = 1234;
$strData = <<< EOF
1234,no,yes,yes
4567,no,yes,yes
8901,yes,no,no
EOF;

if (preg_match("/".$_GET['field1']."\,yes,yes,yes/",$strData))
{
	echo "I work";
}
else if (preg_match("/".$_GET['field1']."\,no\,yes\,yes/",$strData))
{
	echo "I am disabled";
}
else if (preg_match("/".$_GET['field1']."\,yes\,no\,no/",$strData))
{
echo "I am done";
}

Link to comment
Share on other sites

Sure, I would like to have a way to create a new record ie:

4112,yes,yes,yes

 

with a text box for the number field and drop downs for each yes/no

 

This page should also show what already exists in the text file with a way to change the existing records yes/no fields or delete the record completely.

 

Chris

Link to comment
Share on other sites

Something like the following?

<?php

if(isset($_POST['add']))
{
    $new_entry = $_POST['id'];
    $new_entry .= ',' . implode(',',$_POST['option']) . "\n";

    echo 'Adding: ' . $new_entry;

    // write new entry to file
    $handle = fopen('file.txt', 'a'); // open
    fwrite($handle, $new_entry);      // write
    fclose($handle);                  // close
}


?>
<form action="#" method="post">
<table border="0" cellpadding="5" cellspacing="0">
  <tr>
    <th colspan="4">Add New</th>
  </tr>
  <tr>
    <th>ID</th>
    <th colspan="3">Options</th>
  </tr>
  <tr>
    <td><input type="text" name="id" size="10" /></td>
    <td><select name="option[]">
      <option value="yes">Yes</option>
      <option value="no">No</option>
    </select></td>
    <td><select name="option[]">
      <option value="yes">Yes</option>
      <option value="no">No</option>
    </select></td>
    <td><select name="option[]">
      <option value="yes">Yes</option>
      <option value="no">No</option>
    </select></td>
  </tr>
  <tr>
    <td colspan="4"><input type="submit" name="add" value="Add" /></td>
  </tr>
</table>
</form>

 

Change file.txt (in the fopen() function) to your file that contains the data.

Link to comment
Share on other sites

I understand and I greatly appreciate everyone's help so far and this community as a whole. I am with a non-profit Boys & Girls Club and have very little money to spend on things so I've been trying to take a stab at this latest project. I have definately been learning a ton so far on other php pages that I have made but this one I am lost... I can't even find any examples which come close to this. This is why I ask for anyones help that might be able to finish this up or point me in the right direction with an example.

 

Thanks once again,

Chris

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.