Jump to content

Html table iteration with php


Kraus

Recommended Posts

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>

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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


?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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.