proctk Posted September 19, 2007 Share Posted September 19, 2007 Hi The below variables are arrays, I want to create a string from the array values and I'm not sure how to bring them all together $wholeNum = $_POST['wholeNum']; $fraction = $_POST['fraction']; $measure = $_POST['meassure']; $ingredient = $_POST['ingredient']; example $string = $wholeNum.' '.$fraction.' '.$measure.' '.$ingredient Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/ Share on other sites More sharing options...
BlueSkyIS Posted September 19, 2007 Share Posted September 19, 2007 so what happens when you try your example? Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351250 Share on other sites More sharing options...
pikemsu28 Posted September 19, 2007 Share Posted September 19, 2007 $array[] = $_POST['wholeNum']; $array[] = $_POST['fraction']; $array[] = $_POST['meassure']; $array[] = $_POST['ingredient']; $string = implode(' ',$array); echo $string; or $wholeNum = $_POST['wholeNum']; $fraction = $_POST['fraction']; $measure = $_POST['meassure']; $ingredient = $_POST['ingredient']; $array = array($wholeNum, $fraction, $measure, $ingredient); $string = implode(' ', $array); echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351256 Share on other sites More sharing options...
proctk Posted September 19, 2007 Author Share Posted September 19, 2007 thank you for the post. I should have given more information as I cannot get the example posted to work. The variables I posted above gets their values from the form. below any additional help is excellent <table class="nonBorder"> <form name="numItems" method="post" action="<?php $PHP_SELF; ?>"> <tr> <td> <label>Number of ingredients </label><select name="numItems" id="numItems" onchange="this.form.submit()"> <?php $items = $_POST['numItems']; foreach(array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15", "16","17","18","19","20") as $value) { $newValue = 21 - $value; echo "<option value='$newValue'"; if($newValue == $items) { echo " selected='selected'"; } echo ">$value</option>\n"; } ?> </select> </td></tr> </form> </table> <form name="enterReceipt" method="post" action="<?php echo $PHP_SELF; ?>"> <table class="table"> <?php if(isset($_POST['numItems'])){ $numItems = $_POST['numItems']; while ($numItems <= 20) { ?> <tr> <td> <select name="wholeNum[]"> <option value="00">Whole Num</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">0</option> <option value="10">10</option> </select> <select name="fraction[]"> <option value="00">Fraction</option> <option value="1/16">1/16</option> <option value="1/8">1/8</option> <option value="1/4">1/4</option> <option value="1/2">1/2</option> <option value="3/4">3/4</option> </select> <select name="meassure[]" id="measurement"> <option value="">Measure</option> <option value="Teaspoon(s)">Teaspoon(s)</option> <option value="Tablespoon(s)">Tablespoon(s)</option> <option value="Cup(s)">Cup(s)</option> <option value="Pint(s)">Pint(s)</option> <option value="Quart(s)">Quart(s)</option> <option value="Pound(s)">Pound(s)</option> <option value="Pintch">Pintch</option> <option value="Ounce">Ounce</option> </select> <input name="ingredient" type="text" /> <?php $numItems ++; ?> </td> </tr> <?php }}?> <tr><td>Preperation</td></tr> <tr> <td><textarea style="width:470px;" name="preperation" cols="" rows=""></textarea></td> </tr> <tr> <td><label> <input type="submit" name="enterReceipt" id="button" value="Enter Receipt" /> <input type="reset" name="reset" id="reset" value="Reset" /> </label></td> </tr> </table> </form> [code] [/code] Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351262 Share on other sites More sharing options...
pikemsu28 Posted September 19, 2007 Share Posted September 19, 2007 what kind of output are you looking for? i'm not understanding what you want your final result to be. Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351268 Share on other sites More sharing options...
proctk Posted September 19, 2007 Author Share Posted September 19, 2007 I want echo all values stored in the array variables one grouping per line $string = $wholeNum.' '.$fraction.' '.$measure.' '.$ingredient Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351269 Share on other sites More sharing options...
proctk Posted September 19, 2007 Author Share Posted September 19, 2007 Something like this is what I'm trying to do, this does not work as posted because it produces repetitive results if (is_array($wholeNum) || is_array($fraction) || is_array($measure) || is_array($ingredient)){ foreach ($wholeNum AS $key=>$wholeNumValue){ foreach ($fraction AS $key=>$fractionValue){ foreach ($measure AS $key=>$measureValue){ foreach ($measure AS $key=>$ingredient){ $string = $wholeNumValue.' '.$fractionValue.' '.$measureValue.' '.$ingredient; echo $string; } } } } } Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351279 Share on other sites More sharing options...
darkfreaks Posted September 19, 2007 Share Posted September 19, 2007 $string = array($wholeNumValue.' '.$fractionValue.' '.$measureValue.' '.$ingredient); Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351280 Share on other sites More sharing options...
BlueSkyIS Posted September 19, 2007 Share Posted September 19, 2007 ^^^^????? $i=0; foreach ($wholeNum AS $wholeNumValue){ $string = $wholeNumValue.' '.$fraction[$i].' '.$measure[$i].' '.$measure[$i]; echo $string; $i++; } $measure is used twice, so I used it twice in the code. Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351284 Share on other sites More sharing options...
proctk Posted September 19, 2007 Author Share Posted September 19, 2007 that was my mistake the second measure should have been $ingredient. I found the below which I edited. It kind of works but it not grouping for each row I want to keep each row values together 1 1/2 cup milk --> line one 2 cups flour --> line two if(isset($_POST['enterReceipt'])){ $wholeNum = $_POST['wholeNum']; $fraction = $_POST['fraction']; $measure = $_POST['meassure']; $ingredient = $_POST['ingredient']; $preperation = $_POST['preperation']; $n=0; foreach ($wholeNum as $thing) { $bigar[$n][1] = $thing; $n++; } $n=0; foreach ($fraction as $thing) { $bigar[$n][2] = $thing; $n++; } foreach ($measure as $thing) { $bigar[$n][3] = $thing; $n++; } foreach ($ingredient as $thing) { $bigar[$n][4] = $thing; $n++; } foreach ($bigar as $part) { echo $part[1].' '.$part[2].' '.$part[3].' '.$part[4]; } } Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351289 Share on other sites More sharing options...
wildteen88 Posted September 19, 2007 Share Posted September 19, 2007 Try: <?php if(isset($_POST['enterReceipt'])) { echo '<h1>Recipe:</h1> <p> <b>Ingrediants:</b><br /> '; for($i = 0; $i < count($_POST['ingredient']); $i++) { $quantity = $_POST['wholeNum'][$i]; $fraction = ($_POST['fraction'][$i] !== '00') ? $_POST['fraction'][$i] : null; $measurement = ($_POST['meassure'][$i] !== '00') ? $_POST['meassure'][$i] : null; $ingrediant = $_POST['ingredient'][$i]; echo ' <b>' . $quantity . ' ' . $fraction . '</b> ' . $measure . ' ' . $ingrediant . "<br />\n"; } echo '</p> <p> <b>Preperation:</b><br /> ' . $_POST['preperation'] . ' </p>'; } else { ?> <form name="numItems" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <table class="nonBorder"> <tr> <td> <label>Number of ingredients</label> <select name="numItems" id="numItems" onchange="this.form.submit()"> <?php $items = $_POST['numItems']; $range = range(0, 20); foreach($range as $value) { $newValue = 21 - $value; echo '<option value="' . $newValue . '"'; if($newValue == $items) { echo ' selected="selected"'; } echo '>' . $value . "</option>\n"; } ?> </select> </td> </tr> </table> </form> <form name="enterReceipt" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table class="table"> <?php if(isset($_POST['numItems'])) { $numItems = $_POST['numItems']; while ($numItems <= 20) { ?> <tr> <td> <select name="wholeNum[]"> <option value="00">Whole Num</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">0</option> <option value="10">10</option> </select> <select name="fraction[]"> <option value="00">Fraction</option> <option value="1/16">1/16</option> <option value="1/8">1/8</option> <option value="1/4">1/4</option> <option value="1/2">1/2</option> <option value="3/4">3/4</option> </select> <select name="meassure[]" id="measurement"> <option value="">Measure</option> <option value="Teaspoon(s)">Teaspoon(s)</option> <option value="Tablespoon(s)">Tablespoon(s)</option> <option value="Cup(s)">Cup(s)</option> <option value="Pint(s)">Pint(s)</option> <option value="Quart(s)">Quart(s)</option> <option value="Pound(s)">Pound(s)</option> <option value="Pintch">Pintch</option> <option value="Ounce">Ounce</option> </select> <input name="ingredient[]" type="text" /> <?php $numItems ++; ?> </td> </tr> <?php } } ?> <tr><td>Preperation</td></tr> <tr> <td><textarea style="width:470px;" name="preperation" cols="" rows=""></textarea></td> </tr> <tr> <td> <label> <input type="submit" name="enterReceipt" id="button" value="Enter Receipt" /> <input type="reset" name="reset" id="reset" value="Reset" /> </label> </td> </tr> </table> </form> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351296 Share on other sites More sharing options...
proctk Posted September 19, 2007 Author Share Posted September 19, 2007 thank you so much, what you did was excellent and helped me out huge. I have one last challenge with this one. I want to create a long string from the content that makes up the string that you echoed and assign it to a variable which I will use to put this information into a mysql table. the $teststing is the value that to use with my mysql table. $preperation will be added to the table as well but should be only added once I edited your code to this for($i = 0; $i < count($_POST['ingredient']); $i++) { $quantity = $_POST['wholeNum'][$i]; $fraction = ($_POST['fraction'][$i] !== '00') ? $_POST['fraction'][$i] : null; $measurement = ($_POST['meassure'][$i] !== '00') ? $_POST['meassure'][$i] : null; $ingrediant = $_POST['ingredient'][$i]; $StringIngrediant = $quantity . ' ' . $fraction . ' ' . $measure . ' ' . $ingrediant; $testString = $StringIngrediant.'-'; } echo $preperation; } Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351319 Share on other sites More sharing options...
wildteen88 Posted September 19, 2007 Share Posted September 19, 2007 You'll want to use the concatenating assignment operator (.=) instead of a normal assignment operator (=) $StringIngrediant .= $quantity . ' ' . $fraction . ' ' . $measure . ' ' . $ingrediant . '-'; Then echo $StringIngrediant outside of the for loop (before or after the 'echo $preperation; line) Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351399 Share on other sites More sharing options...
proctk Posted September 20, 2007 Author Share Posted September 20, 2007 This is what I have I cannot get the value for the string produced by the for statement to enter into the table if(isset($_POST['enterRecipe'])) { for($i = 0; $i < count($_POST['ingredient']); $i++) { $quantity = $_POST['wholeNum'][$i]; $fraction = ($_POST['fraction'][$i] !== '00') ? $_POST['fraction'][$i] : null; $measurement = ($_POST['meassure'][$i] !== '00') ? $_POST['meassure'][$i] : null; $ingrediant = $_POST['ingredient'][$i]; $stringIngrediant = $quantity . ' ' . $fraction . ' ' . $measure . ' ' . $ingrediant; } $query_add_recipe = ("INSERT INTO recipes (title, description, ingredients, preperation, servings, yield, PreparationTime, cookTime, user_id, add_date) VALUES('$title', '$description', '$stringIngrediant', '$preperation', '$numServing', '$yield', '$prepTime', '$cockTime', '$user_id', now())"); mysql_query($query_add_recipe)or die("SQL Error: $query_add_recipe<br>" . mysql_error()); header("Location: addRecipe.php"); } else { Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351522 Share on other sites More sharing options...
wildteen88 Posted September 20, 2007 Share Posted September 20, 2007 You're not using the concatenating assignment operator (.=) as I suggested in my post above. Re-read my post above for what to do. Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351662 Share on other sites More sharing options...
proctk Posted September 21, 2007 Author Share Posted September 21, 2007 Thank you for your help. I fixed it up and all is well. Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-351949 Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 please paste the final code and press topic solved if you havent already Quote Link to comment https://forums.phpfreaks.com/topic/69935-solved-string-from-array-value/#findComment-352039 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.