index.php coding

<td><input type="text" name="slno[]" value="" size="2"></td>
      <td><input type="text" size="10" name="date[]" id="SelectedDate" onClick="GetDate(this);" readonly="readonly"/></td>
      <td><input size="24" type="text" name="particulars[]" value="" placeholder="Description"></td>
      <td><select name="noofnights[]" value="0">
      <option value="empty"></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">9</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
      <option value="13">13</option>
      <option value="14">14</option>
      <option value="15">15</option>
      <option value="16">16</option>
      <option value="17">17</option>
      <option value="18">18</option>
      <option value="19">19</option>
      <option value="20">20</option>
      <option value="21">21</option>
      <option value="22">22</option>
      <option value="23">23</option>
      <option value="24">24</option>
      <option value="25">25</option>
      <option value="26">26</option>
      <option value="27">27</option>
      <option value="28">28</option>
      <option value="29">29</option>
      <option value="30">30</option>
      <option value="31">31</option>
    </select>
        <input size="14" type="text" onKeyUp="return valtxt(this)" name="rate[]" value="0">
        <input size="14" type="text" name="price[]" value="0" readonly="readonly">
<input size="9" type="text" name="tax[]" onKeyUp="return valtxt(this)" value="0">
<input size="1" type="hidden" name="taxtotal[]" value="0" readonly="readonly"></td>

store.php coding

if (isset($_POST['submit_val']))
{
foreach ( $_POST['slno'] as $key=>$slno )
{
$date = $_POST['date'][$key];
$particulars = $_POST['particulars'][$key];
$c_date = mysql_real_escape_string($date);
$c_slno = mysql_real_escape_string($slno);
$tariff = mysql_query("INSERT INTO ebvouchertariffs ( TariffSlNo, TariffDate)", $link );
}
}

here i stored some values to mysql db using an array. i used dynamic rows so i used an array for store the values into database. now i want to fetch the values from mysql db. how can i do that using foreach?? in this database table i assigned one id from another table. for example if i inserted 5 rows into the table i assigned specific id from another table for these 5 rows. so i want to use that specific id and then fetch the 5 rows values. i tried but i cannot able to get 5 rows. first row value only fetched....

Recommended Answers

All 5 Replies

Member Avatar for diafol

Why are you providing arrays in ALL your controls? For example...

<input type="text" size="10" name="date[]" id="SelectedDate" onClick="GetDate(this);" readonly="readonly"/>

Using an array value for name attribute doesn't make much sense. As you have a general 'id' value, I'm assuming there's only one of these in your form. So your control should read...

<input type="text" size="10" name="date" id="SelectedDate" onClick="GetDate(this);" readonly="readonly"/>

In addition, your styling and scripting are mashed in with your html. It is good practice to separate these as far as possible - it will also help contributors who wish to help you with your code. So if using HTML5, you could cut down on the verbosity of your markup thus...

<input name="date" id="SelectedDate" readonly />

The sizing can do in the CSS and setup a listener for the control...

$('#SelectDate').click(function(){
    //jQuery flavour
});

or

document.getElementById("SelectDate").onclick=function(){
    //vanilla js
};

That last bit is not directly answering your question I know, but hopefully it will help too.

Below is the detail use of foreach with PDO Query

<?php


// We Will prepare SQL Query
    $STM = $dbh->prepare("SELECT `SrNo`, `TTDescription`, `TTCity`, `TTEntryDate`, `TTEntryTime`, `TTEntryBy`, `TTClosedDate`, `TTClosedTime`, `TTClosedBy`, `Status` FROM ttmain WHERE Status='Open' AND TTEntryDate = SUBDATE(CURDATE(),1)");
// For Executing prepared statement we will use below function
    $STM->execute();
// we will fetch records like this and use foreach loop to show multiple Results
    $STMrecords = $STM->fetchAll();
    foreach($STMrecords as $row)
        {


         echo"<tr>";
         echo"<td>".$row[0]."</td>";
         echo"<td>".$row[1]."</td>";
         echo"<td>".$row[2]."</td>";
         if($row[3]!='0000-00-00') {   echo "<td>" . date("j-M-Y",strtotime($row[3])) ."</td>"; } else { echo "<td>NA</td>"; }
         if($row[4]!='00:00:00') {   echo "<td>".$row[4]."</td>"; } else { echo "<td>NA</td>"; }
         echo"<td>".$row[5]."</td>";
         if($row[6]!='0000-00-00') {   echo "<td>" . date("j-M-Y",strtotime($row[6])) ."</td>"; } else { echo "<td>-</td>"; }
         if($row[7]!='00:00:00') {   echo "<td>".$row[7]."</td>"; } else { echo "<td>-</td>"; }
         if($row[9]=='Closed') {   echo "<td>".$row[8]."</td>"; } else { echo "<td>-</td>"; }
         if($row[9]=='Closed') {   echo "<td><span class='label label-success'>".$row[9]."<span></td>"; } else { echo "<td><span class='label label-important'>".$row[9]."</span></td>"; }
         echo"</tr>";
        }               
            ?>
<?php
if (isset($_POST['submit_val'])){
    $values ="";
    foreach ( $_POST['slno'] as $key=>$slno ){
        $date = $_POST['date'][$key];
        $particulars = $_POST['particulars'][$key];
        $c_date = mysql_real_escape_string($date);
        $c_slno = mysql_real_escape_string($slno);
        $values .= "('".$c_slno."','".$c_date."'),";

    }
            $values = substr($values,0, strlen($values)-1); // This will remove the last comma in the values string
            //echo "INSERT INTO ebvouchertariffs ( TariffSlNo, TariffDate) VALUES ".$values;
            $tariff = mysql_query("INSERT INTO ebvouchertariffs ( TariffSlNo, TariffDate) VALUES ".$values, $link );
}
?>

I suppose this will help you.

@ sasankasekhar : i dont want to insert a row. i want to fetch the datas from mysql... i already inserted rows into mysql.. there is nothing error on the above code.. i inserted using foreach (dynamic javascript rows) so i want to fetch using foreach.

In this code:
$tariff = mysql_query("INSERT INTO ebvouchertariffs ( TariffSlNo, TariffDate)", $link );
Where is your $link variable coming from? It appears you are discarding all of the values submitted via the $_POST array, or at least not using them in your database. To help you with writing a query and loop to get the data out, we need to know what data is going in and how it gets there, to have a better idea of the DB structure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.