Jump to content

[SOLVED] Check for Repeated Entry


oceans

Recommended Posts

Dear People,

 

May you please help me in “Check for Repeated Entry”.

Situation:

I have 5 TextBoxes, I need to make sure All Entries are NOT the SAME, I should be able to make my own function, but if there is such function in PHP, I can use it before sweating right.

 

I may appear easy, to process 5 Boxes, it involves creative looping, I believe.

 

Link to comment
Share on other sites

$boxes = array('box1' => "$_POST['box1']", 
'box2' => "$_POST['box2']", 
'box3' => "$_POST['box3']", 
'box4' => "$_POST['box4']", 
'box5' => "$_POST['box5']");

$unique = array_unique($boxes);

$count = count($unique);

if($count < 5){
  // this is bad
}else{
  //this is good
}

Link to comment
Share on other sites

Dear Friend,

 

There is a problem,

 

My code:

 

"

 

$boxes = array('Txt1' => "$_POST['Txt1']",

'Txt2' => "$_POST['Txt2']",

'Txt3' => "$_POST['Txt3']",

'Txt4' => "$_POST['Txt4']",

'Txt5' => "$_POST['Txt5']");

 

$unique = array_unique($boxes);

 

$count = count($unique);

 

if($count < 5)

{

// this is bad

echo "Reapeated Entry Found!";

{

else

{

  //this is good

  echo "Reapeated Entry NOT Found!";

}

 

"

error on screen

"

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\MyPHP\Member\Untitled-1.php on line 68

"

 

I believe you are using "Pointers". I am honest, I am not good at "Pointers", thus I can't understand the error either, can you help me.

Link to comment
Share on other sites

Dear People,

 

Thanks for the breaces:

 

My code

 

"

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?PHP

//Transfer Data from Screen to Memory

$NumberOfTxtBoxes=5;

$NumberOfTxtBoxesToBeFilled=5;

$theValue = "No";

if (isset($_POST['Submit']))

{

for ($i=1; $i<=$NumberOfTxtBoxes; $i++)

{

$InputFromScreen[$i]=strtoupper($_POST["Txt".$i]);

}

}

else

{

for ($i=1; $i<=$NumberOfTxtBoxes; $i++)

{

$InputFromScreen[$i]="";

}

}

 

  $boxes = array('Txt1' => "$_POST['Txt1']",

  'Txt2' => "$_POST['Txt2']",

  'Txt3' => "$_POST['Txt3']",

  'Txt4' => "$_POST['Txt4']",

  'Txt5' => "$_POST['Txt5']");

 

  $unique = array_unique($boxes);

 

  $count = count($unique);

 

  if($count < 5)

  {

    // this is bad

    echo "Reapeated Entry Found!";

  }

  else

  {

    //this is good

    echo "Reapeated Entry NOT Found!";

  } 

?>

<form id="form1" name="form1" method="post" action="Untitled-1.php">

  <table width="200" border="1">

    <tr>

      <td> </td>

      <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt33" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td>

    </tr>

    <tr>

      <td> </td>

<td><select name="Combo2" size="1" class="WorkPageField" id="Combo2">

        <option value="Yes" <? if($theValue == "Yes"){ echo "selected"; } ?>>Yes</option>

        <option value="No" <? if($theValue == "No"){ echo "selected"; } ?>>No</option>

      </select></td>

    </tr>

    <tr>

      <td> </td>

      <td><select name="select" size="1" class="WorkPageField" id="select">

        <option value="Yes">Yes</option>

        <option value="No">No</option>

      </select></td>

    </tr>

    <tr>

      <td> </td>

      <td><?PHP

?></td>

    </tr>

    <tr>

      <td> </td>

      <td><input type="submit" name="Submit" value="Submit" /></td>

    </tr>

  </table>

</form>

</body>

</html>

 

"

 

Error on screen

 

"

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\MyPHP\Member\Untitled-1.php on line 29

 

"

Link to comment
Share on other sites

Try replacing this part

 

 $boxes = array('Txt1' => "$_POST['Txt1']",
   'Txt2' => "$_POST['Txt2']",
   'Txt3' => "$_POST['Txt3']",
   'Txt4' => "$_POST['Txt4']",
   'Txt5' => "$_POST['Txt5']");

 

with this

 

 $boxes = array('Txt1' => $_POST['Txt1'],
   'Txt2' => $_POST['Txt2'],
   'Txt3' => $_POST['Txt3'],
   'Txt4' => $_POST['Txt4'],
   'Txt5' => $_POST['Txt5']);

Link to comment
Share on other sites

the first thing that comes to mind is a nested foreach loop.

<?php
        $bool = false;
        foreach($_POST as $key1 => $val1){
                foreach($_POST as $key2 => $val2){
                        (($val1 == $val2) ? ($bool == true) : (''));
                }
        }
        echo (($bool == true) ? ("this is bad.\n") : ("this is good"));
?>

 

but i'm sure there's a much better way to do it. but that should work in the meantime.

 

EDIT: that will not work because it will always end up comparing itself with itself, so $bool will always eventually be true. i'll edit this post with another function when i figure it out.

 

i think you had it right with array_unique:

<?php
        if(!array_unique($_POST)){
                echo "not good.\n";
        }else{
                echo "this is acceptable.\n";
        }
?>

Link to comment
Share on other sites

Dear Benjaminbeazy,

 

I think I missleasd you. What I ment was.

Situation:

We have five boxes, three are filled uniquely, the other two left empty.

Your code will respond all the five boxes are not unique, because of the empty boxes.

How can we get arround this "inovating your code" to ignore the boxes if it is empty.

Link to comment
Share on other sites

i posted the relevant code, u can replace as necessary

 

foreach($boxes as $var => $val){

    if(strlen(ltrim(rtrim($val))) < 1){

        unset($boxes[$var]);
    }

}

   
if($count(array_unique($boxes)) < count($boxes))
   {
    // this is bad
    echo "Reapeated Entry Found!";
   }
   else
   {
     //this is good
     echo "Repeated Entry NOT Found!";
   } 

Link to comment
Share on other sites

I tried

"

 

$boxes = array('Txt1' => $InputFromScreen[1],

  'Txt2' => $InputFromScreen[2],

  'Txt3' => $InputFromScreen[3],

  'Txt4' => $InputFromScreen[4],

  'Txt5' => $InputFromScreen[5]);

foreach($boxes as $var => $val)

{

    if(strlen(ltrim(rtrim($val))) < 1)

{

        unset($boxes[$var]);

    }

}

if($count(array_unique($boxes)) < count($boxes))

  {

    // this is bad

    echo "Reapeated Entry Found!";

  }

  else

  {

    //this is good

    echo "Repeated Entry NOT Found!";

  }

 

 

"

 

error on screen

"

 

Fatal error: Function name must be a string in C:\wamp\www\MyPHP\Member\Untitled-1.php on line 44

 

"

 

line 44 is "if($count(array_unique($boxes)) < count($boxes))"

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.