Jump to content

strpos and preg_match and arrays oh my!


leoric80

Recommended Posts

Hi I have a web form that posts the following to my PHP page..

<span align="left" class="label fix">To:</span><br><input type="text" name="destno"><br>

This is the PHP page that receives the input...

$destno = $_POST['destno'];

//Year 7 message to all
if ($destno == "Year7")
         {

$result = mysql_query("select ".$school_id."_students.year, ".$school_id."_contacts.p1contact_no from  ".$school_id."_students inner join ".$school_id."_contacts on ".$school_id."_contacts.contact_id_internal=".$school_id."_students.student_id_internal and ".$school_id."_students.year = 7;");
        while ($row = mysql_fetch_assoc($result)) {
$p1contact_year = $row["p1contact_no"];

 mysql_query("INSERT INTO ".$school_id."_outbox (senderno, destno, message, student_id)
 VALUES ('$senderno', '$p1contact_year', '$message', '$student_id')");


}

}

This works perfectly fine however I need to statically define the  value  like this......

 

if ($destno == "Year7")

 

What I would like to do is pull the data from my database and store in an array, I have no problem with that either...

 

The problem is that I need to check the array for a matching string for $destno, I cant seem to do this with strpos and I think I have to use preg_match to achieve this but after hours of trying I am getting nowhere fast.

 

Could anyone possibly tell me how they would do this or even better still provide me with an example if possible?

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/279368-strpos-and-preg_match-and-arrays-oh-my/
Share on other sites

you shouldn't rely on a text input field when trying to match specific values in a database. you should query the database table for the possible values and output the selections in an appropriate format on the form - multi-select, checkboxes... the value(s) submitted by the form would be a set of the possible values in the database table.

If you want to see if a value exists in an array of values, then use in_array. If your array is multi-dimensional, use a foreach loop and a boolean variable.

Hi Thanks for that, I think that sounds about right...I have fumbled a good few attempts and wondered if you could show me an example of in_array with a foreach loop and boolean?

 

I am using the following to build my array....

$reg_group=mysql_query('select distinct class from '.$school_id.'_students');
while ($Row = mysql_fetch_array($reg_group)) {
$class = $Row['class'];

Sorry its probably obvious what I need to do but its been a long day!

you shouldn't rely on a text input field when trying to match specific values in a database. you should query the database table for the possible values and output the selections in an appropriate format on the form - multi-select, checkboxes... the value(s) submitted by the form would be a set of the possible values in the database table.

Yeah I realise that, I was hoping to come up with a more dynamic way of using the entries that exist in the database somehow rather than searching for a pre-defined string as the entries will vary hugely and I cannot predict what they will be :( Thanks

I am using the following to build my array....

$reg_group=mysql_query('select distinct class from '.$school_id.'_students');
while ($Row = mysql_fetch_array($reg_group)) { 
$class = $Row['class'];

 

That code does not create any array. It just overwrites $class each time. If you want $class to be an array of all the returned class values you'd use:

$class[] = $Row['class'];

 

Then, to see if $destno exists in that array:

if (in_array($destno, $class)){
   //It exists
}
else {
   //Could not be found
}

That code does not create any array. It just overwrites $class each time. If you want $class to be an array of all the returned class values you'd use:

$class[] = $Row['class'];

 

Then, to see if $destno exists in that array:

if (in_array($destno, $class)){
   //It exists
}
else {
   //Could not be found
}

 

 

Thank you so much, I have this working now :) I appreciate your time

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.