Jump to content

please help with the logic for this. I cant figure it out :(


kevinkhan

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
   
   }
   }
}



?>

Link to comment
Share on other sites

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

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.