Jump to content

concat array data?


leoric80

Recommended Posts

Hey

 

i am using the following code which works great for me currently...basically I am using in_array to search an array which is generated by a web form ($post) 

$reg_group=mysql_query('select distinct class from '.$school_id.'_students');
while ($Row = mysql_fetch_array($reg_group)) {
$class[] = $Row['class'];
//reg groups
elseif (in_array($destno, $class))
         {


$result = mysql_query("select ".$school_id."_students.class, ".$school_id."_students.student_id_internal, ".$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 where class = '$destno';");
        while ($row = mysql_fetch_assoc($result)) {
$p1contact = $row["p1contact_no"];
$student_id_int = $row["student_id_internal"];

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

}

}

However what I would like to do now is search an array but instead of looking at one value (in this case $class) I would like to concat the values into one string to search for.

 

eg. The array contains (first_name,last_name,class), I would like to join the results together to it appears as one string (firstnamelastnameclass) then use in_array to search for that string.

 

Hopefully that makes some kind of sense?

 

Any help would be gratefully appreciated.

 

Cheers

 

 

 

Link to comment
Share on other sites

if you are building your original (class) array, on the same page where your form submits, for producing a menu/select list of some type, so that the array exists anyway, then testing the submitted data against the entries in that array as a step in validating the submitted data is no problem.

 

however, if you are building this array just to search through it to test if the submitted value is one of the choices, this is not the way to do it, more so with multiple fields as the distinct query you need to build the menu won't work with multiple fields. retrieving all the values to test if they contain one value is inefficient compared to just testing the values directly in a query.

Link to comment
Share on other sites

if you are building your original (class) array, on the same page where your form submits, for producing a menu/select list of some type, so that the array exists anyway, then testing the submitted data against the entries in that array as a step in validating the submitted data is no problem.

 

however, if you are building this array just to search through it to test if the submitted value is one of the choices, this is not the way to do it, more so with multiple fields as the distinct query you need to build the menu won't work with multiple fields. retrieving all the values to test if they contain one value is inefficient compared to just testing the values directly in a query.

 

 

Thanks. I was just using that as an example of how I have achieved this previously (sorry i wasnt very clear on that). Basically I want to run a query that fetches a number of fields and then joins those fields into one string if possible? I need the string to match the value of all three fields combined not just one of the fields/values.

Link to comment
Share on other sites

while ($row=...){
$array[] = $row['field1'].$row['field2'].$row['field3']; //concatenate fields, save string in array
}

if (in_array($search, $array)){
//...
}
You could probably just do your search in your sql query too and not have to deal with the arrays, but you'd need to provide more details about what you're searching for and why, how the table is, etc. Edited by kicken
Link to comment
Share on other sites

i would separate the concatenated fields with a delimiter so that combinations of letters between the fields are guaranteed to be unique.

 

billiejohn doe someclass - billiejohndoesomeclass vs billiejohn|doe|someclass

billie johndoe someclass - billiejohndoesomeclass vs billie|johndoe|someclass

 

if your rows have an auto-increment id, why not just use the id (identifier) to identify what data has been submitted?

Edited by mac_gyver
Link to comment
Share on other sites

[off-topic] while this doesn't have anything to do with your question in this thread, you need to use alias names in your query. this will simplify, clean up, and reduce the repetition in your queries. you can also include php variables directly in a double-quoted string, thereby further reducing your queries - 

$query = "select s.class, s.student_id_internal, c.p1contact_no from {$school_id}_students s inner join {$school_id}_contacts c on c.contact_id_internal= s.student_id_internal where class = '$destno'";

 you should also form your query statements in a php variable. this helps during debugging (you can echo/log the actual query) and makes switching to a different database library easier, which you need to do since the mysql_ functions have been depreciated starting in php5.5 and new code should be written using the mysqli_ or PDO database libraries.

 

and are you sure you want to have separate tables for each school_id? what happens when a student moves and transfers to a different school or a contact has students in more than one school? you either end up shuffling around data and making sure it got moved correctly or duplicating data between tables. both are bad design practices that can result in data errors and out of sync information.

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.