Jump to content

How to display the entire table?


forum
Go to solution Solved by ginerjm,

Recommended Posts

8 minutes ago, Barand said:

Самый простой способ - обработать $query как массив и просто перебирать результаты

<таблица>
    <?php foreaach ($query как $row) {echo "<tr><td>" . Присоединиться("</td><td>", $row) . "</td></tr>";
        }
    ?>
</table>

 

somehow you wrote everything confusingly, but why doesn’t it display the entire table as I wrote? because it's clear and simple.

Link to comment
Share on other sites

Have you done any reading of how to write php and how to do querying and how to display the results?  There's some nice examples in the official PHP manual that would have answered your question immediately, if you read it.

(Tip:  You cannot echo an array nor a resource result from a query. You have to break it down to the contents which Barand did with a function but it could have been done with more detail which might have been clearer to you.)

Link to comment
Share on other sites

22 minutes ago, ginerjm said:

Вы читали что-нибудь о том, как писать php, как выполнять запросы и как отображать результаты? В официальном руководстве по PHP есть несколько хороших примеров, которые сразу же ответили бы на ваш вопрос, если бы вы его прочитали.

(Совет: вы не можете отобразить массив или результат ресурса из запроса. Вы должны разбить ее на содержимое, которое Barand сделал с помощью функции, но это можно было бы сделать с более подробной информацией, которая могла бы быть вам понятнее.)

I read, but a lot is not clear, if the database has a table consisting of rows and columns, why renew it into a row. I can’t understand this)) if the table already consists of a row? how can this be understood?

Link to comment
Share on other sites

Tables consist of records, or rows.   Each record of a table has a set of fields or data items, also called columns.  A properly designed table makes it easy to search for data based on a properly written query statement.  Once you have created that query and run the query function, you can then write a loop that usually uses a Fetch call to retrieve a single row, outputs or analyzes the data on that row and continues on until there are no rows left to be fetched.

Do not think of this as any kind of spreadsheet.   Tables are part of a Database.  Spreadsheets are not.   One table can be related to another table and allow you to write queries that gather data from several tables into one result set that gives you pieces of data from that database in a fashion that allows you to do what you need to do with that data.

Edited by ginerjm
Link to comment
Share on other sites

2 hours ago, ginerjm said:

Tables consist of records, or rows.   Each record of a table has a set of fields or data items, also called columns.  A properly designed table makes it easy to search for data based on a properly written query statement.  Once you have created that query and run the query function, you can then write a loop that usually uses a Fetch call to retrieve a single row, outputs or analyzes the data on that row and continues on until there are no rows left to be fetched.

Do not think of this as any kind of spreadsheet.   Tables are part of a Database.  Spreadsheets are not.   One table can be related to another table and allow you to write queries that gather data from several tables into one result set that gives you pieces of data from that database in a fashion that allows you to do what you need to do with that data.

But if the table is already created in the database, why complicate the code so much to display the entire table? For example, in a group of 100 students, not a single student understands this complex record and it is not clear how to explain it, it remains to be said, you just need to write it down, of course, it’s stupid, but we don’t understand.

Link to comment
Share on other sites

7 minutes ago, forum said:

But if the table is already created in the database, why complicate the code so much to display the entire table?

When you issue a query it doesn't give you an HTML table of data as the result as that would be pretty useless.  The data is stored in memory in some format that may or may not even resemble a table, and the result you get is an object that allows you to access that memory.

There are plenty of other things you might want to do with that data besides display it in a HTML table.  Maybe you want to put it into a table in a PDF, or use the data to generate emails, or just fill out specific parts of a page, or ....  So it doesn't make sense for PHP to just auto-generate an HTML table out of a result set.

If you want to display all that data as a HTML table, then you need to generate the appropriate HTML to create that table. 

Link to comment
Share on other sites

42 minutes ago, forum said:

But if the table is already created in the database, why complicate the code so much to display the entire table? For example, in a group of 100 students, not a single student understands this complex record and it is not clear how to explain it, it remains to be said, you just need to write it down, of course, it’s stupid, but we don’t understand.

1 -  your data is stored in a database table but that doesn't mean it can simply be displayed with something that just "writes it down".  Only records that contain some data values/fields that don't have labels on the pieces that you want to view and which need some code to help make that presentation.

2 - working with data that has been properly stored in a database table is fairly simple when you know how to use a language to collect it and display it.  It may be called 'complicated' but that is only because it is new to you.   PHP is a fairly easy language to learn and use but it does take some time to read up on and some practice writing it and debugging it until you get the hang of it.

3 - what is this group of 100 students you are referring to?  I (we?) feel that we are communicating with just one person who may be considered a student who needs to learn a bit about how PHP produces the desired output format, aka, an HTML table.  If one wants to read data from a source and display it in a nice fashion one has to learn how to write the proper commands that will do that.

4 - complex record?  Records are basically not complex to those who work with them.  You can learn that, although I don't think it is handled by simply writing it down.

Hope this helps you and your 100 students a bit.

Link to comment
Share on other sites

$query = $db->query("SELECT * FROM msg");

// Start an HTML table to display the results
echo "<table>";
echo "<tr><th>ID</th><th>Message</th><th>Sender</th></tr>";

// Loop through the rows of data and add them to the table
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo "<tr>";
    echo "<td>" . $row["id"] . "</td>";
    echo "<td>" . $row["message"] . "</td>";
    echo "<td>" . $row["sender"] . "</td>";
    echo "</tr>";
}

// Close the HTML table
echo "</table>";

This code uses an HTML table to display the query results, with column headings for each field. Inside the while loop, each row of data is output as an HTML table row (<tr>) with cells for each column (<td>). The final echo statement closes the HTML table. Note that this code assumes that the columns in the msg table are named id, message, and sender. If your column names are different, you should update the code to reflect the correct column names.

Link to comment
Share on other sites

20 hours ago, ginerjm said:

1 -  your data is stored in a database table but that doesn't mean it can simply be displayed with something that just "writes it down".  Only records that contain some data values/fields that don't have labels on the pieces that you want to view and which need some code to help make that presentation.

2 - working with data that has been properly stored in a database table is fairly simple when you know how to use a language to collect it and display it.  It may be called 'complicated' but that is only because it is new to you.   PHP is a fairly easy language to learn and use but it does take some time to read up on and some practice writing it and debugging it until you get the hang of it.

3 - what is this group of 100 students you are referring to?  I (we?) feel that we are communicating with just one person who may be considered a student who needs to learn a bit about how PHP produces the desired output format, aka, an HTML table.  If one wants to read data from a source and display it in a nice fashion one has to learn how to write the proper commands that will do that.

4 - complex record?  Records are basically not complex to those who work with them.  You can learn that, although I don't think it is handled by simply writing it down.

Hope this helps you and your 100 students a bit.

I always look at the result. This is a language I have been studying for 2 years, and still I don’t understand how many students do some moments, that’s why I ask questions because I don’t understand it,

Link to comment
Share on other sites

21 hours ago, Barand said:

You think two lines of code is an over-complicated solution?

Yes, this code format is hard to remember, it seemed that everything would be simple, but no, everything is somehow complicated

Link to comment
Share on other sites

16 hours ago, Strider64 said:
$query = $db->query("SELECT * FROM msg");

// Start an HTML table to display the results
echo "<table>";
echo "<tr><th>ID</th><th>Message</th><th>Sender</th></tr>";

// Loop through the rows of data and add them to the table
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo "<tr>";
    echo "<td>" . $row["id"] . "</td>";
    echo "<td>" . $row["message"] . "</td>";
    echo "<td>" . $row["sender"] . "</td>";
    echo "</tr>";
}

// Close the HTML table
echo "</table>";

This code uses an HTML table to display the query results, with column headings for each field. Inside the while loop, each row of data is output as an HTML table row (<tr>) with cells for each column (<td>). The final echo statement closes the HTML table. Note that this code assumes that the columns in the msg table are named id, message, and sender. If your column names are different, you should update the code to reflect the correct column names.

Oh, I'm completely confused here, I won't be able to write this for a long time))))

Link to comment
Share on other sites

Actually this last post of the possible code for you is SO simple.  It is using very little PHP and more of HTML.

1 - Query - the command that runs a query statement to produce the results you want to see

2 - echo - a statement that sends output to the client

3 - while - a loop starting statement which allows one to repeatedly process a given set of data.  In this case that data is a result row from the query.  The code included in this loop is wrapped, aka, contained, by a set of braces, ie, {}.

4 - Fetch - the command that returns one row of the query results to you for processing

Look up these commands in the manual.  If you don't know how yet, here is the link to the Functions chapter of the manual: 

    https://www.php.net/manual/en/funcref.php

There is a search box/field in the upper right of the page.  Have at it.  So simple - just three php commands being used.  Now how long can that possible take to learn.  Basically this little block of code runs a query to :

   - collect some data;

   - begin an html table

   - start  loop on the query results that gives the script one row to read at a time

   - the loop code outputs a line of an html table   The table elements (HTML) are <table>, <tr>, <th> and <td>.

   - at the end of the loop the html table is closed.

The rest of the code is using more echo statements to send the necessary HTML table elements to the client to present the output in a readable format.  I am going out on a limb and stopping here since I hope you understand html.  

Edited by ginerjm
Link to comment
Share on other sites

Here is a slight reworking of the previous code that might make it easier to follow:

$sql = "SELECT * FROM msg";	// write a query
$results = $db->query($sql);	// run the query using a database connection 
				// already made somewhere previously

// Start an HTML table to display the results
echo "<table>";
echo "<tr>
		<th>ID</th>
		<th>Message</th>
		<th>Sender</th>
		</tr>";		// output the table headings row

// Loop through the rows of data and add them to the table
while ($row = $results->fetch(PDO::FETCH_ASSOC)) 
{	// begin the loop code
	//  process the currently fetched row of query results
	echo "<tr>";	// start an html table row
	echo "<td>" . $row["id"] . "</td>";	// put an element into the row
	echo "<td>" . $row["message"] . "</td>";	// another element
	echo "<td>" . $row["sender"] . "</td>";		// another element
	echo "</tr>";		// end the current row
}	// end of the while loop

// Close the HTML table
echo "</table>";
exit();

Note the use of semicolons at the end of each php line except the while line which relies on the {} to end the line.  Note also the use of periods to concatenate elements of the echo statements.  Note the user of the brackets and quotes to access each element of the $row array (which fetch provides you).

Don't know what else I can do to make this easier to comprehend other than a strong recommendation to RTFM.   Programmers have to do that.

Edited by ginerjm
Link to comment
Share on other sites

1 hour ago, ginerjm said:

Actually this last post of the possible code for you is SO simple.  It is using very little PHP and more of HTML.

1 - Query - the command that runs a query statement to produce the results you want to see

2 - echo - a statement that sends output to the client

3 - while - a loop starting statement which allows one to repeatedly process a given set of data.  In this case that data is a result row from the query.  The code included in this loop is wrapped, aka, contained, by a set of braces, ie, {}.

4 - Fetch - the command that returns one row of the query results to you for processing

Look up these commands in the manual.  If you don't know how yet, here is the link to the Functions chapter of the manual: 

    https://www.php.net/manual/en/funcref.php

There is a search box/field in the upper right of the page.  Have at it.  So simple - just three php commands being used.  Now how long can that possible take to learn.  Basically this little block of code runs a query to :

   - collect some data;

   - begin an html table

   - start  loop on the query results that gives the script one row to read at a time

   - the loop code outputs a line of an html table   The table elements (HTML) are <table>, <tr>, <th> and <td>.

   - at the end of the loop the html table is closed.

The rest of the code is using more echo statements to send the necessary HTML table elements to the client to present the output in a readable format.  I am going out on a limb and stopping here since I hope you understand html.  

Yes, I know html, but I also thought that I knew PHP, but I decided to display the entire table and it turned out that I don’t know how to do it,))) I thought that I would write the variable into which we make the request in the echo, and that’s all, and then it turned out that not everything is so simple, the brain does not want to perceive what is already in the row table, but they need to be connected through a sampling cycle, so many actions are not clear why, I understood when it was necessary to compare something line by line or something look in the table, but why such difficulties in the usual output here is not yet possible to understand, you just need to agree this is how it should be)

Link to comment
Share on other sites

Huh? Not sure what you are trying to say.  But - did my last post help at all to make it clearer what and why the code should look like that?

FYI - If you think that this little exercise is a lot of code you should realize that most php programmers write this kind of code all of the time, surrounded by a lot of other code that creates the data thru manipulation, calculation or concatenation with other data.  This is programming.  And wait until you get to the point that you want to write really fancy looking output and have to learn CSS.  And then add some JS or JQ to add some client-based interactions with the user. 

That is what you have to look forward to.

Edited by ginerjm
Link to comment
Share on other sites

15 minutes ago, ginerjm said:

Huh? Not sure what you are trying to say.  But - did my last post help at all to make it clearer what and why the code should look like that?

FYI - If you think that this little exercise is a lot of code you should realize that most php programmers write this kind of code all of the time, surrounded by a lot of other code that creates the data thru manipulation, calculation or concatenation with other data.  This is programming.  And wait until you get to the point that you want to write really fancy looking output and have to learn CSS.  And then add some JS or JQ to add some client-based interactions with the user. 

That is what you have to look forward to.

I understand that this is very difficult. I wrote the code as it is clear to me and the result is also there,
$query=$db->query("SELECT * FROM msg");
foreach($query as $row){
     echo "$row[id]: $row[msg]: $row[login]: $row[time_msg]<br>";
}

although for me it would not suit) but what to do if it was invented to write this php code

Link to comment
Share on other sites

4 minutes ago, ginerjm said:

Did you Read what I already posted before (or after) you wrote the recent post?  It is not correct.

And once again I don't understand what you are writing in that last line.

yes, i read your post, in the last line i output all the values from the table into columns.

Link to comment
Share on other sites

You didn't follow what I wrote then.  I specifically told you to use quotes on the array elements which you are not doing.

And NO - you did not output all the values ... into columns.  There is no html formatting there at all.  How can you even think that you have put anything into a column???

I think you have more than a php problem.

I made my post as simple as one can write this exercise in order to help you to follow my explanation as to what you have to do here.  If you can't read it and follow it then I think my work here is wasted.

Edited by ginerjm
Link to comment
Share on other sites

43 minutes ago, ginerjm said:

You didn't follow what I wrote then.  I specifically told you to use quotes on the array elements which you are not doing.

And NO - you did not output all the values ... into columns.  There is no html formatting there at all.  How can you even think that you have put anything into a column???

I think you have more than a php problem.

I made my post as simple as one can write this exercise in order to help you to follow my explanation as to what you have to do here.  If you can't read it and follow it then I think my work here is wasted.

I realized that now it’s very difficult to explain to beginners that they understood and remembered the form of writing code, I personally don’t understand this now, maybe when I don’t understand how to write correctly, but it’s all difficult and not clear, for example, as I wrote code, it's a bit clear to me what's going on in the code and it's easier to remember.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.