Kraus Posted May 28, 2018 Share Posted May 28, 2018 Hi all!! I need a little help with the following php code. I hope it's very simple for you, experts. I create an html table from tableA in mysql with the following values. and from another mysql table (tableB), I have saleID. What I need is to create this html table as many as the number of saleID in tableB. So, if the number of saleID is 10, then I'd have 10 html tables on the page.(yes, they are all same, and small tables) if it is possible, It'd be great to have saleID in <table id=""> of the html table. I'd really appreciate if someone can help me a little here. Thanks! <table class="style" id=""> <?php $conn=mysqli_connect("localhost","root","","datadb"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql = "SELECT Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8 FROM tableA "; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "</td><td>". $row["Value1"] . "</td><td>" . $row["Value2"]. "</td><td>" . $row["Value3"]."</td><td>" . $row["Value4"]."</td><td>" . $row["Value5"]. "</td><td>" . $row["Value6"]."</td><td>" . $row["Value7"]."</td><td>" . $row["Value8"]. "</td></tr>"; } echo "</table>"; } $conn->close(); ?> </table> Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2018 Share Posted May 28, 2018 As you have given no details of the table structures, other than table B contains a saleid and there is no is no sign of such a column in tableA, then I cannot give you much guidance. All I can say is that if there is a common column then use it to JOIN the tables in your query. (If those really are the column names in tableA then you seriously need to read up on data normalization) Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 thanks, Barand. The tableA structure is like this: unitID, Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8. Basically tableA will have the same values always, and it won't be updated or changed anything. and unitID is not necessary in this case. since two tables are not related. From tableB, only saleID is needed, nothing else. this won't change in time either. I don't need JOIN function here. I create an HTML table from tableA with this code. I need a little adjustment to create multiples of the same table on the page according to the number of saleID. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 28, 2018 Share Posted May 28, 2018 (edited) Your html code for the table is no good. You begin with a </td> in your loop. Huh? And just why do you want to produce multiple identical tables that have no reference to this saleid value? Perhaps you want to explain the problem to be solved rather than ask us for a solution that is impossible to create without fully understanding the task. Edited May 28, 2018 by ginerjm Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 Yes, I need to change </td>. it is a mistake. I need to create this html table as the number of saleID. tableB has other columns which are related to the HTML table. but it is not relevant in making of this html table. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2018 Share Posted May 28, 2018 I am still no wiser about how you intend processing table B. However I would put the table printing code in a function then call it as required passin the id from table B function print_table_A ($conn, $id) { $sql = "SELECT Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8 FROM tableA "; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table id='$id'>"; while($row = $result->fetch_assoc()) { echo '<tr><td>' . join('</td><td>', $row) . "</td></tr>\n" ; } echo "</table>"; } } Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 It didn't produce any results, but empty page. then I did a little addition to your code, but result didn't change. this is what I tried. <?php $conn=mysqli_connect("localhost","root","","datadb"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $id="SELECT saleID FROM tableB"; function print_table_A ($conn, $id) { $sql = "SELECT Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8 FROM tableA "; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table id='$id'>"; while($row = $result->fetch_assoc()) { echo '<tr><td>' . join('</td><td>', $row) . "</td></tr>\n" ; } echo "</table>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 tableB has other data/columns which I want to use with these tables. saleID is basically an id, differentiator and the count of rows. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 28, 2018 Share Posted May 28, 2018 Obviously you don't have php error checking turned on or are not looking for errors in the error log. One cannot output an array var directly - you have to break it down, which I am SURE you wish to do here. So you apparently have a table that has a set of saleid records. And you are querying them all here. And for each of those saleid values (ie, row) you want to select a set of 'values' that belong to it. But then you want to display html tables of those values without any indication of what saleid they belong to. Have I got it yet? And what is the user supposed to do with this display? Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 yes, something like this. I need to count the number of saleIDs from tableB, so if it is 40 for example, then I need to create 40 HTML tables according to the values in tableA. tableA has fixed number of values. I already have the code to create the HTML table. I just need a piece of code to say that count the number of saleID in tableB, and then do the same process that number of times. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2018 Share Posted May 28, 2018 (edited) Quote $id="SELECT saleID FROM tableB"; What do you think that line does? Is the saleid unique to each record in table B or are there several records for each saleid. If the latter do you only want the table printed for each separate id value? As I said, you've told us nothing. Edited May 28, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 (edited) It selects a saleID from tableB? With or without this line I got the same result. empty page really. didn't make any difference. Edited May 28, 2018 by Kraus Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2018 Share Posted May 28, 2018 No, it doesn't. It puts that query string into id. You have actually to query the database to get the value. Similarly, just defining the function doesn't give you the table, you have to call the function. Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 Quote Is the saleid unique to each record in table B or are there several records for each saleid. yes, saleID is unique to each row. Sorry, I tought since it is an ID, I thought you'd assume that way. So there is one record /row for each saleID in tableB. Quote If the latter do you only want the table printed for each separate id value? As I said, you've told us nothing. since saleID's are unique, it needs to be counted, and one table for each row should be printed. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2018 Share Posted May 28, 2018 Quote Sorry, I tought since it is an ID, I thought you'd assume that way. An id can also be a foreign key, say in a table of sales transactions with hundreds of records for each saleid. Perhaps $result = $conn->query("SELECT saleID from tableB"); while ($row = $result->fetch_assoc()) { print_table_A ($conn, $row['saleID']); } Quote Link to comment Share on other sites More sharing options...
Kraus Posted May 28, 2018 Author Share Posted May 28, 2018 yes, it works now. it generates tables as the number of saleIDs. thanks, Barand. Quote Link to comment 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.