0

Just learning this stuff, so bear with any ignorance...

In what I'm trying to do, users input a word and as output receive their word as letter artwork that I've stored in a mysql database. I have a php page that breaks the characters of this word into an array and then matches these letter characters with tagged records.

What I'm having trouble with is getting the outputted images (a) into the same order as the characters of the user-inputted word and (b) choosing (randomly) only one tagged image (among many) per input letter.

Thanks for any suggestions or help on scripting this!


Here are some ideas I've cobbled together with some help...

$cxn = mysqli_connect($host, $user, $password, $database) or die("no cxn"); 
$lettertype = str_split($_POST['letter']);
array_walk($lettertype, 'mysql_real_escape_string');
$lettertype = "'" . implode("','", $lettertype) . "'";
$query = "SELECT * FROM images WHERE letter IN ($lettertype)";
$result = mysqli_query($cxn, $query)
        or die ("No good");
    while($row = mysqli_fetch_assoc($result))
        {
        echo "<a href='../delete3/images/{$row['imagePath']}' border='0'>
        <img src='../delete3/images/{$row['imagepath']}' width='100' height='80'/></a>";
        }
3
  • 1
    Help us help you by posting more info for example things like ur DB structure? you existing code? example of an simple input the the returned result (which will be wrong according to you) Commented Oct 28, 2011 at 1:39
  • Surely you must have something in your output that aligns with the input? you could str_split() the input word, and then for each character in that array, loop through your results array with a straight forward comparision buiding the finaly array in order. Commented Oct 28, 2011 at 1:51
  • Thanks for taking the time. I added the critical parts above. Commented Oct 28, 2011 at 2:19

1 Answer 1

1

You will have to test this yourself...

$alphasoup = array();
while($row = mysqli_fetch_assoc($result))
{
  $alphasoup[$row['letter']][] = $row;
}
foreach(str_split($_POST['letter']) as $alpha)
{
echo "<a href='../delete3/images/{$alphasoup[($alpha][0]['imagePath']}' border='0'>
<img src='../delete3/images/{$alphasoup[$alpha][0]['imagepath']}' width='100' height='80'/></a>";
}

Bonus Edit: and if you want the ability to have multiple graphics for the same letter and to pick one at random, store...

$alphasoup[$alpha][array_rand($alphasoup[$alpha])]['imagepath']

...as single variable to re-use in both instances in your string

2
  • Also it will be important that the user input has the same character case. Commented Oct 28, 2011 at 2:40
  • Exactly what I was trying to find! Solved. Tried it out and it works like a champ. Thanks for the awesome effort. Commented Oct 28, 2011 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.