Jump to content

[SOLVED] string from array value


proctk

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;


}

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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
{

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.