kevinkhan Posted February 2, 2012 Share Posted February 2, 2012 i have an array called $imageArray like this Array ( [0] => 'Dashka'_Darren_Sheehan_1.jpg [1] => 'Dashka'_Darren_Sheehan_10.jpg [2] => 'Dashka'_Darren_Sheehan_2.jpg [3] => 'Dashka'_Darren_Sheehan_3.jpg [4] => 'Dashka'_Darren_Sheehan_4.jpg [5] => 'Dashka'_Darren_Sheehan_5.jpg [6] => 'Dashka'_Darren_Sheehan_6.jpg [7] => 'Dashka'_Darren_Sheehan_7.jpg [8] => 'Dashka'_Darren_Sheehan_8.jpg [9] => 'Dashka'_Darren_Sheehan_9.jpg [10] => . [11] => .. [12] => Aaron_Kidney_1.jpg [13] => Aaron_Kidney_10.jpg [14] => Aaron_Kidney_11.jpg [15] => Aaron_Kidney_12.jpg [16] => Aaron_Kidney_13.jpg [17] => Aaron_Kidney_14.jpg [18] => Aaron_Kidney_15.jpg [19] => Aaron_Kidney_16.jpg [20] => Aaron_Kidney_17.jpg [21] => Aaron_Kidney_18.jpg and i also have another array called $_names Array ( [0] => 'Dashka'_Darren_Sheehan [1] => Beefit_Gyms [2] => Aaron_Kidney [3] => Dean_Mitchell [4] => Colin_O_Neill [5] => Keith_McManus ) if a name in array $_names exists in the $imageArray i want to insert the images for that person into one column of a table like this <tr> <?php foreach ($names as $name): ?><th><?php htmlout($name); ?></th><?php endforeach; ?> </tr> <tr> <td><img src="../images/'Dashka'_Darren_Sheehan_1.jpg" width="204"/></td> <td><img src="../images/Aaron_Kidney_1.jpg" width="204"/></td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td><img src="../images/'Dashka'_Darren_Sheehan_2.jpg" width="204"/></td> <td><img src="../images/Aaron_Kidney_2.jpg" width="204"/></td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> can anyone help me write the logic for this?? I cant figure it out... Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 2, 2012 Share Posted February 2, 2012 So, the image names will ALWAYS be a name followed by an underscore and a number? Where are you getting these arrays? Are they generated from a database or a file processing routine? I think there is probably a simpler solution to handle this at the time the arrays are created instead of after the fact. I could provide a solution, but I want to see where the data is coming from first so I'm not providing the wrong solution. Quote Link to comment Share on other sites More sharing options...
wigpip Posted February 2, 2012 Share Posted February 2, 2012 Hi PHP folks this is the logic I came up with so basically foreach within a foreach and then use strpos function and if a match echo out the image, so here's the bones of the code. the first foreach just gets the last name from the names array assuming it's the last after the last underscore. <?php foreach($_names as $value) { $parts = explode('_',$value); $parts = count($parts); $key = $parts; $last_name = $_names[$key]; foreach($imageArray as $matching_value) { if(strpos($last_name,$matching_value)) { echo $matching_value; } } } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 3, 2012 Share Posted February 3, 2012 Yes, you can go with that approach, but as I was trying to find out there is probably a more efficient way to do this with the initial creation of the arrays rather than after the fact. Quote Link to comment Share on other sites More sharing options...
kevinkhan Posted February 3, 2012 Author Share Posted February 3, 2012 Yes, you can go with that approach, but as I was trying to find out there is probably a more efficient way to do this with the initial creation of the arrays rather than after the fact. The above code doesnt seem to work. The first array $_names is created from names in a textarea box and the $imageArray is created from the names of all images in an images folder here is my complete code which might be more helpful. <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/facebook/photoApp/security/includes/helpers.inc.php'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Results</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <body> <table border="1"> <tr> <?php foreach ($names as $name): ?><th><?php htmlout($name); ?></th><?php endforeach; ?> </tr> <tr> <td><img src="../images/corkpartydjs@gmail.com/Aidan_Lambert_1.jpg" width="204"/></td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td><img src="../images/corkpartydjs@gmail.com/Aidan_Lambert_2.jpg" width="204"/></td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td><img src="../images/corkpartydjs@gmail.com/Aidan_Lambert_3.jpg" width="204"/></td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td><img src="../images/corkpartydjs@gmail.com/Aidan_Lambert_4.jpg" width="204"/></td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> </table> <p><a href="?">New search</a></p> </body> </html> and here is the processing file <?php include_once $_SERVER['DOCUMENT_ROOT'] .'/facebook/photoApp/security/includes/functions.inc.php'; if (isset($_GET['action']) and $_GET['action'] == 'search') { $user = $_GET['user']; $names = $_GET['names']; $names = nl2br($names); //red($names); $names = str_replace ( '<br />' , "\t" , $names ); $_names = str_replace( " " , "_" , $names); //blue($_names); $_names = explode("\t",$_names); $names = explode("\t",$names); // print_r($_names); echo "<pre>"; print_r($_names); echo "</pre>"; if ($handle = opendir($_SERVER['DOCUMENT_ROOT'] .'/facebook/photoApp/images/'.$user)) { // echo "Directory handle: $handle\n"; //echo "Entries:\n"; /* This is the correct way to loop over the directory. */ $imageArray = array(); while (false !== ($entry = readdir($handle))) { array_push($imageArray, $entry); } } sort($imageArray); echo "<pre>"; //print_r($imageArray); echo "</pre>"; foreach($_names as $value) { $parts = explode('_',$value); blue(count($parts)); $numberOfParts = count($parts); $last_name = $parts[$numberOfParts-1]; blue($last_name); foreach($imageArray as $matching_value) { if(strpos($last_name,$matching_value)) { echo $matching_value; } } } include $_SERVER['DOCUMENT_ROOT'] . '/facebook/photoApp/security/results.html.php'; exit(); } // Display search form include $_SERVER['DOCUMENT_ROOT'] . '/facebook/photoApp/security/includes/db.inc.php'; $result = mysqli_query($link, 'SELECT email FROM facebook_user'); if (!$result) { $error = 'Error fetching facebook users from database!'; include 'error.html.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $users[] = array('email' => $row['email']); } include 'searchform.html.php'; ?> Thanks for your help guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.