Jump to content

[SOLVED] Lions, Tigers and Arrays Oh My!


eRott

Recommended Posts

Hey,

 

Alright so here's my problem. Basically a user is given a form to to fill out. In one of the sections of this form they need to list multiple 5-digit numbers. Sometimes it could be as little as 2 5-digit numbers, or as many as 10 5-digit numbers. Creating a bunch of individual fields and corresponding rows in a MySQL database wouldn't really work seeing as I am unsure how many 5-digit numbers a user may enter. So I figured the only way to go about it, would be to use an array. My aim is to have a single field where a user just lists the multiple 5-digit numbers separated by some means (a comma seems to be the most common). From there I need to be able to take each separate 5-digit number (however many there are), and output each one in via PHP. Essentially, those numbers will complete a URL.

 

So here's an example.

 

The field in the MySQL database would look something like:

19562, 96025, 02856, 11954, 70533

 

From there, I would need to output each individual 5-digit number to complete a URL.

<?php
// where number would be one of the 5-digit numbers
$number = ???;

// This would need to be in a while loop I belive so it will echo each 5-digit number
echo '<a href="http://www.domain.com/?item='.$number.'">Item</a>';
?>

 

I am not too sure how much sense any of this makes, but I really don't know where to begin in order to better explain the problem. So any guidance or suggestions would be a big help. Thanks.

 

Regards,

eRott

Link to comment
https://forums.phpfreaks.com/topic/118973-solved-lions-tigers-and-arrays-oh-my/
Share on other sites

Its pretty simple, u just need one function and a loop:

 

<?php
$csv = "19562, 96025, 02856, 11954, 70533"; //this may come from post or db or whatever
$numbers = explode(', ', $csv); //explode creates an array of the numbers split by the delimiter: ', '
foreach($numbers as $val){
     echo "<a href='http://www.domain.com/?item=$val'>Item</a>";
}
?>

 

Hope that helps.

Perfect. That would appear to do the trick. Thank you kindly. I do however, have one more question. What if I wanted to include another variable in that URL but each number has to match up to a name in the same corresponding order? To replace the value in between the <a> tag along with the item number?

 

There would be two options here I would think. Either A) simply creating a second field and applying the same method you write above using something like (would it work?):

 

<?php
$csv_numbers = "19562, 96025, 02856, 11954, 70533"; // this would come from the DB
$csv_names = "apple, orange, carrot, grape, tomato"; // this would come from the DB

$numbers = explode(', ', $csv_numbers); //explode creates an array of the numbers split by the delimiter: ', '
$names = explode(', ', $csv_names); //explode creates an array of the names split by the delimiter: ', '

foreach($numbers as $item_id && $names as $item_name)
     echo "<a href='http://www.domain.com/?item=".$item_id."'>".$item_name."</a>";
}
?>

 

Or, B) would it be possible to combine it all into one big array/csv where the pattern would be number, name, number, name etc etc and output it where the number and the following name would match up in a URL?

 

19562, apple, 96025, orange, 02856, carrot, 11954, grape, 70533, tomato

 

So in the end, I would want to end up with something like:

 

<a href="http://www.domain.com/?item=19562">apple</a>
<a href="http://www.domain.com/?item=96025">orange</a>
<a href="http://www.domain.com/?item=02856">carrot</a>
<a href="http://www.domain.com/?item=11954">grape</a>
<a href="http://www.domain.com/?item=70533">tomato</a>

 

What are your thoughts?

 

Thanks a million.

Take care.

Having 2 different array isnt a good idea. There's not way of smartly associating them, just by its position and it could easily break your script flow. What i think is the best way, should be:

 

number|fruit, number|fruit

 

To parse those things, you can use this code:

 

<?php
$csv = "19562|apple, 96025|orange, 02856|carrot, 11954|grape, 70533|tomato"; //
$fields = explode(', ', $csv); //explode the fields by the ', ' delimiter. It will give: [0] = > "19562|apple", [1] => "96025|orange", etc...
foreach($fields as $val){ //loop through the array
list($number, $name) = explode('|', $val); //get the number and name by exploding the "|"
echo "<a href='http://www.domain.com/?item=" . $number . "'>" .$name. "</a><br />";
}
?>

Hope this clears up your ideas 

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.