11 Replies - 746 Views - Last Post: 05 December 2016 - 10:32 AM Rate Topic: -----

#1 sinux12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 06-December 14

How to create table data line one by one

Posted 30 November 2016 - 11:35 PM

I'm doing a small project which is small PHP program to manage yarn costing I've done most of it but I'm stuck with one problem which I really don't know how to solve so here is the description of problem.

What I'm trying to do is once the user submit the yarn ID through AJAX the all the other details will be grabbed from the database and will be sent back to create a on data row in the table "Yarn Details" after populating the first yarn user will add another and another ... etc all these data will be editable will be submitted later to another data base table.

Below is my coding for the form Ajax and the for the PHP.

Form HTML
<body>
<form id="form1" name="form1" method="post" action="Process/cstDetailsAdd.php">
    <label for="fileNo">File No : </label><input type="text" name="fileNo" id="fileNo" />&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="sampleNo">Sample No : </label><input type="text" name="sampleNo" id="sampleNo" />&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="cstSheetNo">Cost Sheet No : </label><input type="text" name="cstSheetNo" id="cstSheetNo" />
    <br class="clear" />
    <br class="clear" />
    <label for="csDate">Date : </label><input type="text" name="csDate" id="csDate" />&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="catID">Category ID : </label><input type="text" name="catID" id="catID" />&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="cusName">Customer Name: </label><input type="text" name="cusName" id="cusName" />&nbsp;&nbsp;&nbsp;&nbsp;
    <br class="clear" />
    <br class="clear" />
    <label for="lblName">Label Name : </label><input type="text" name="lblName" id="lblName" />&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="lblLength">Label Length: </label><input type="text" name="lblLength" id="lblLength" />&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="lblWidth">Label Width: </label><input type="text" name="lblWidth" id="lblWidth" />&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="numRepeats">No. of Repeats: </label><input type="text" name="numRepeats" id="numRepeats" />&nbsp;&nbsp;&nbsp;&nbsp;
    <br class="clear" />
    <br class="clear" />
    <label>Add Yarn: </label><input type="text" size="15" id="yarnSearch" name="yarnSearch" onkeyup="getYarn(this.value)">
    <input type="button" value="Add" id="" onclick="getDetails()">
    <div id="results" style="position: absolute; left: 75px; right: auto; width: 120px;"></div>
    <br class="clear" />
    <br class="clear" />
    <label>Yarn Details : </label>
    <div id="showDetails"></div>
</form>
</body>



Code AJAX:
include_once ("DBConnect/iConnect_db.php");

function getDetails()
{
    yarnSearch=document.getElementById("yarnSearch").value;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("showDetails").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","getYarn.php?p="+yarnSearch,true);
    xmlhttp.send();
}



Code PHP:
$getID = "SELECT * FROM yarndetails WHERE yarnId = :yarnId";
$dbDetailSql = $dbConnect -> prepare($getID);
$dbDetailSql -> bindParam(':yarnId', $_GET['p']);
$dbDetailSql -> execute();

$rowDetails = $dbDetailSql -> fetch(PDO::FETCH_ASSOC);

echo "<table id='yarnTable' width='100%'>";
echo     "<tr>";
echo           "<th>yId</th>";
echo            "<th>Yarn ID</th>";
echo            "<th>Denier</th>";
echo            "<th>Description</th>";
echo            "<th>Status</th>";
echo            "<th>Picks</th>";
echo            "<th>Yarn Price</th>";
echo            "<th>Cost</th>";
echo            "<th>No.of Tapes</th>";
echo      "</tr>";
echo       "<tr align='center'>";
echo            "<td>".$rowDetails['yId']."</td>";
echo            "<td><input size='6' type='text' id='yarnID' name='yarnID' value='".$rowDetails["yarnId"]."'/></td>";
echo            "<td><input size='6' type='text' id='denier' /></td>";
echo            "<td><input size='25' type='text' id='decrip'></td>";
echo            "<td><input size='10' type='text' id='status'></td>";
echo            "<td><input size='6' type='text' id='picks'></td>";
echo            "<td><input size='8' type='text' id='yarnPrice'></td>";
echo            "<td><input size='8' type='text' id='cost'></td>";
echo            "<td><input size='6' type='text' id='noOfTapes'></td>";
echo            "<script type='application/javascript'></script>";
echo            "<td><input type='button' value='Delete' id='delYarn' onclick='delRow(this)'></td>";
echo        "</tr>";
echo    "</table>";



Above code works but it only populates one data row I know it's not the right method but I really don't know how to get the code to create function to add data line by line so please help I love to learn how to do it thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: How to create table data line one by one

#2 chorn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 20-June 16

Re: How to create table data line one by one

Posted 01 December 2016 - 12:22 AM

at least your have to request all data

$dbDetailSql -> fetch_all


and run over it with e.g. `foreach` to create every row.
Was This Post Helpful? 0
  • +
  • -

#3 sinux12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 06-December 14

Re: How to create table data line one by one

Posted 01 December 2016 - 01:20 AM

Thank you will try out your suggestion. The thing is the data table is not created in one go user enters data (yarn ID) one by one when he press add button the extra details has to be taken from the data base then added to the next row of the same table this is where I'm drawing a blank right now.

This post has been edited by Dormilich: 01 December 2016 - 01:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 chorn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 20-June 16

Re: How to create table data line one by one

Posted 01 December 2016 - 02:14 AM

you may

session_start()


at the top of your scripts that interact with the database. When the user adds a new ID, you

$_SESSION['ids'][] = (int)$id;


that to have a list of all IDs. Than you can query the database like

select foo, bar from tab where id in(1,2,3)


where the list of IDs may come from

implode(',', $_SESSION['ids']);

Was This Post Helpful? 0
  • +
  • -

#5 sinux12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 06-December 14

Re: How to create table data line one by one

Posted 01 December 2016 - 02:21 AM

View Postchorn, on 01 December 2016 - 02:44 PM, said:

you may

session_start()


at the top of your scripts that interact with the database. When the user adds a new ID, you

$_SESSION['ids'][] = (int)$id;


that to have a list of all IDs. Than you can query the database like

select foo, bar from tab where id in(1,2,3)


where the list of IDs may come from

implode(',', $_SESSION['ids']);


Thank you, but my question is not about creating sessions I have created a session checker which is working fine this different.
Was This Post Helpful? 0
  • +
  • -

#6 chorn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 20-June 16

Re: How to create table data line one by one

Posted 01 December 2016 - 03:04 AM

then i don't get what your problem with

Quote

user enters data (yarn ID) one by one


is.
Was This Post Helpful? 0
  • +
  • -

#7 sinux12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 06-December 14

Re: How to create table data line one by one

Posted 01 December 2016 - 03:39 AM

View Postchorn, on 01 December 2016 - 03:34 PM, said:

then i don't get what your problem with

Quote

user enters data (yarn ID) one by one


is.


yarn ID is a the reference ID for a particular yarn or sawing strings all the details for that yarn is in table called yarndetails in a MySQL data base. When a user types in a yarnID and click on the Add button I want the script get all the details of that particular yarn ID and put them in to a HTML table populating one row then user enters another yarID which will be added as another populated data row use has to be able enter any amount of yarn ID's which will be show in a table.
Was This Post Helpful? 0
  • +
  • -

#8 chorn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 20-June 16

Re: How to create table data line one by one

Posted 01 December 2016 - 03:47 AM

still don't get the problem, that's what for i postet the session storage.

Quote

that to have a list of all IDs. Than you can query the database like

Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,064
  • Joined: 15-January 14

Re: How to create table data line one by one

Posted 01 December 2016 - 11:12 AM

It sounds like your page should start with an empty table instead of an empty div. When you send the ajax response back you shouldn't send all of the HTML, only send the database data as a JSON object. In the Javascript you get the JSON and turn it back into an object representing the database row, and then use the DOM methods to create a new row in the existing table and add cells to it with whatever contents you want in each cell, where you use the database data to fill in the contents.
Was This Post Helpful? 0
  • +
  • -

#10 sinux12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 06-December 14

Re: How to create table data line one by one

Posted 01 December 2016 - 09:53 PM

View PostArtificialSoldier, on 01 December 2016 - 11:42 PM, said:

It sounds like your page should start with an empty table instead of an empty div. When you send the ajax response back you shouldn't send all of the HTML, only send the database data as a JSON object. In the Javascript you get the JSON and turn it back into an object representing the database row, and then use the DOM methods to create a new row in the existing table and add cells to it with whatever contents you want in each cell, where you use the database data to fill in the contents.


Thank you @ArtificialSoldier I do understand what you're saying but can you educate me on JSON (I'm a noob when it comes to those new stuff) and DOM I don't need copy paste code just an example what to do would be grate I'm trying to learn not to copy paste.
Was This Post Helpful? 0
  • +
  • -

#11 sinux12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 06-December 14

Re: How to create table data line one by one

Posted 04 December 2016 - 10:18 PM

I managed to find how to do this (Thank you @ArtificialSoldier for pointing me the right way) but have small question I'm new to using JSON found this tutorial which shows how to create a json object. So my question is where should I put the below code ??

The code is not mine it's from the tut just want to know if I write something like this where should have this code in.

Should I have this in my form.php or Ajax.js or in Action.php (process PHP file).

var myObject = JSON.parse(httpxml.responseText);



var str="<table width='200'  align=center>";

for(i=0;i<myObject.data.length;i++)

{ 

str = str + "<tr bgcolor='#f1f1f1'><td >ID:</td><td>" + myObject.data[i].id + " </td></tr>";

str = str + "<tr ><td >Name:</td><td>" + myObject.data[i].name + " </td></tr>";



str  = str + "<tr bgcolor='#f1f1f1'><td>Class</td><td>"+ myObject.data[i].class1 + "</td></tr>";

str  = str + "<tr><td>Mark</td><td>"+ myObject.data[i].mark + "</td></tr>";

}



str = str + "</table>" ;



document.getElementById("display").innerHTML=str;


Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,064
  • Joined: 15-January 14

Re: How to create table data line one by one

Posted 05 December 2016 - 10:32 AM

That's Javascript code, so you would run that code when Javascript receives the ajax response from the PHP page.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1