I have seen some examples here, but none of them are really doing what I want. I can have up to 100 rows according to bale numbers in the database that I am pulling from. I have the backend working and all, but the problem is the rows just span across horizontally when I want them to show as new rows for each bale number (the new row starts after each exit weight). What's the best/correct way to go about this? I am trying to do all this logic using PHP. If I just wrap the td tags in my PHP for each with tr tags, then I just get the a long vertical display of data... I'm stuck. Thanks!
You can see the html, my JS and PHP are (separate files):
HTML
<table id="table" align="center">
<thead>
<tr>
<th>Bale #</th>
<th>Cable #</th>
<th>Shipping #</th>
<th>GP Cable Type</th>
<th>Exit Weight</th>
</tr>
</thead>
<tbody id="bale_data">
</tbody>
</table>
JS
function baleData() {
$.ajax({
url: './php/baleData.php',
success: function(data) {
$('#bale_data').append(data);
},
error: function(xrs, thrownError, xhr) {
alert("Ajax Error" + xrs.status + thrownError + xhr.status);
}
});
};
PHP
//Create array
$column = array("bale_no","cable_no","shipment_no","gp_cable_type","exit_weight");
//Define JSON array
$array = array();
//Run SQL for each array value
for ($i=1; $i<=100; $i++) {
foreach ($column as $value) {
$sql="SELECT $value FROM bundle_lanes WHERE bale_no='$i'";
$result = mysql_query($sql) or die ('Error'.mysql_error());
while ($data = mysql_fetch_array($result)) {
print "<td><input value=\"".$data[$value]."\"></td>";
}
}
}
//Return array
print json_encode($array);
You're making more calls than you really need, and you're not creating a new row anywhere. This will be only 1 query per $i, though you can lessen that as well by limiting your query to just 100.
$column = array("bale_no","cable_no","shipment_no","gp_cable_type","exit_weight");
//Define JSON array
$array = array();
//Run SQL for each array value
for ($i=1; $i<=100; $i++) {
$sql="SELECT ".implode(',',$column)." FROM bundle_lanes WHERE bale_no='$i'";
$result = mysql_query($sql) or die ('Error'.mysql_error());
while ($data = mysql_fetch_array($result)) {
print "<tr>";
foreach($column as $value)
print "<td><input value=\"".$data[$value]."\"></td>";
print "</tr>";
}
}
If you want to get rid of that foreach, change your query to
$sql="SELECT ".implode(',',$column)." FROM bundle_lanes LIMIT 100";
On this part of the code:
print "<td><input value=\"".$data[$value]."\"></td>";
You are creating just the table data portion of your table. You need to create a row tr
per result of your SQL, and append several td
for each of the objects data.
while ($data = mysql_fetch_array($result)) {
print "<tr>";
print "<td><input value=\"".$result[$attribute1]."\"></td>";
print "<td><input value=\"".$result[$attribute2]."\"></td>";
print "<td><input value=\"".$result[$attribute3]."\"></td>";
// etc...
print "</tr>";
}
P.S.: The above code is an example of how you should do it. You must adapt it to your context.
Since you have 5 collumns, you can add a counter that counts each cell you add and after each 5 cells reset the counter and create a new line. Here's an example:
$counter = 0;
//Run SQL for each array value
for ($i=1; $i<=100; $i++) {
foreach ($column as $value) {
if($counter == 0){
print "<tr>"; //Create the new row
}
$sql="SELECT $value FROM bundle_lanes WHERE bale_no='$i'";
$result = mysql_query($sql) or die ('Error'.mysql_error());
while ($data = mysql_fetch_array($result)) {
print "<td><input value=\"".$data[$value]."\"></td>";
$counter++; //After we added a new cell we update the counter
}
if($counter == 5){ //After each 5 added cells we end the new row and reset the counter
print "</tr>";
$counter = 0;
}
}
}