I have a database loaded with different churches information, I am trying to insert all the information from the database into a PHP table with 3 rows.
I would like the structure of each cell to be:
Church Name
Image
Pastor Name
I can easily insert all data into a table, but I cannot get it to display as 3 rows.
echo("<table>");
while($row = mysql_fetch_array($rs))
{
echo("<tr>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("</tr>");
}
echo("</table>");
Doing this causes me to have 3 rows in correct structure, but having duplicate data. I understand that I have not changed the id, but am not sure what I should do
You're repeating the data on the row. If you want to show 3 items per row, you need to add a counter and a statement to draw the table row breaks, as below:
$int_Col = 1;
echo("<table>");
while($row = mysql_fetch_array($rs))
{
if ($int_Col == 1) {
echo("<tr>");
}
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
if ($int_Col == 3) {
echo("</tr>");
$int_Col = 1;
} else {
$int_Col++;
}
}
if ($int_Col > 1) {
echo("<td colspan='". (3 - $int_Col) ."'> </td></tr>");
}
echo("</table>");
The last check ($int_Col > 1) should ensure the table is rendered properly with an empty cell - should span the correct amount of cells that were not drawn on the current row.
Just clarify, I think there's two definitions of "rows" here.
MySQL table rows
HTML table rows
I believe you're after getting 3 MySQL table rows into one HTML table row.
Use the following to split the HTML row every 3 MySQL rows:
$i = 0;
while($row = mysql_fetch_array($rs))
{
// whatever you're already doing(ish)
$i++;
if($i % 3 == 0)
{
echo('</tr><tr>');
}
}
You'll need to put some extra checks in in the event that the total number of MySQL rows doesn't divide exactly by 3 because in that case you'll have one or two empty cells to fill at the end of the table.
If I get your code right you are creating 3 rows for every row in the database. mysq_fetch will run the loop for every single row, so the right content of the loop would be:
echo("<tr>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("</tr>");
Related
I have data, from HTTP requests, that I am rendering in the form of a table. Right now, I just have it so that the listings are ordered as the for loop processes them, but I want it so that the for loop processes them and then sorts the data according to highest % of weight. Weight is a column header in a table wit h7 other column headers.
Now, this project is in php + html. I have never used php before, so am rather confused with syntax and where to put logic.
I have tried using the sort() and asort() functions in php, but I am clearly putting these things in the wrong order. I feel like what I want to do is: 1) save the output of the for_loop that renders the table in a table format as some data type 2) then sort that data type according to the column of weight in ascending order. 3) display this in a table
Here is some code!
"<div class='last-updated text-left'>" .
"<small>As of <span>$update_date</span></small>" .
"</div>" .
"<table class='ershares-table with-thead'>" .
"<thead><tr>" .
"<th>Company</th>" .
"<th>Weight</th>" .
"<th>Ticker</th>" .
"<th>Market Price</th>" .
"<th>Shares Held</th>" .
"<th>Market Value</th>" .
"<th>CUSIP</th>" .
"</tr></thead> " .
"<tbody>";
for ($i = 0; $i < $for_loop_limit; $i++) :
echo
"<tr>" .
"<td><strong>" . $ers_holdings[$i]["securityDescription"] . "</strong></td>" .
"<td>" . getMarketValuePercentage($ers_holdings[$i]["securityIdentifier"], $ers_holdings[$i]["marketValuePercentage"]) . "%</td>" .
"<td>" . $ers_holdings[$i]["ticker"] . "</td>" .
"<td>" . $ers_holdings[$i]["marketPriceOfSecurity"] . "</td>" .
"<td>" . $ers_holdings[$i]["sharesHeldOfSecurity"] . "</td>" .
"<td>" . $ers_holdings[$i]["marketValueOfHolding"] . "</td>" .
"<td>" . $ers_holdings[$i]["securityIdentifier"] . "</td>" .
"</tr>";
endfor;
echo "</tbody>" .
'</table>' .
I want to sort the table by having the highest value for getMarketValuePercentage... at the top. Thank you in advance.
You're looking for usort().
Before doing your foreach loop, you'll call something along the lines of:
usort($ers_holdings, function ($a, $b) {
return ($a['securityIdentifier'] <=> $b['securityIdentifier']);
});
which would sort the data in $ers_holdings based on the ['securityIdentifier'] value.
I am fairly new to working with PHP and WordPress. I tried exploring how to escape and sanitize, and I got a little confused along the way.
I'd like to echo out the contents of an entire table from the database. I am unsure whether there is a better way of both creating a table in a more structured way, and I can't figure out how to escape the data when I don't select specific data from the database. Maybe i'm just confused. Any help or pointers is highly appreciated.
I found the code somewhere on Stack Overflow, edited a little and tried to understand it. I understand it now, but I am still confused on where to go from here.
<?php
$results = $wpdb->get_results( "SELECT * FROM user"); // Query to fetch data from database table and storing in $results
if(!empty($results)) // Checking if $results have some values or not
{
echo "<table width='100%' border='0' id='userTable'>"; // Adding <table> and <tbody> tag outside foreach loop so that it wont create again and again
echo "<tbody>";
echo "<tr>"; // Adding rows of table inside foreach loop
echo "<th>E-mail</th>" . "<th>Fornavn</th>" . "<th>Efternavn</th>" . "<th>Registreret den</th>";
echo "</tr>";
foreach($results as $row){ //putting the user_ip field value in variable to use it later in update query
echo "<td colspan='3'><hr size='2'></td>";
echo "<tr>";
echo "<td>" . esc_attr($row->email) . "</td>" . "<td>" . $row->firstname . "</td>" . "<td>" . $row->lastname . "</td>" . "<td>" . $row->signuptime . "</td>"; //fetching data from user_ip field
}
echo "</tbody>";
echo "</table>";
}
?>
This part...
foreach($results as $row){
//putting the user_ip field value in variable to use it later in update query
echo "<td colspan='3'><hr size='2'></td>";
echo "<tr>";
echo "<td>" . esc_attr($row->email) . "</td>" . "<td>" . $row->firstname . "</td>"
. "<td>" . $row->lastname . "</td>" . "<td>" . $row->signuptime . "</td>";
//fetching data from user_ip field
}
...would product html like
{3 columns}{content}{3 columns end}{row start}
{column start}{content}{column end} * 4
{3 columns}{row start}
{column start}{content}{column end} * 4
{3 columns}{row start}
{column start}{content}{column end} * 4
etc
What you want in your loop is probably:
{row start}{4 columns}{content}{4 columns end}{row end}
{row start}{column start}{content}{column end} * 4{row end}
which would look like this:
foreach($results as $row){
//putting the user_ip field value in variable to use it later in update query
echo "<tr><td colspan='4'><hr size='2'></td></tr>";
echo "<tr><td>" . esc_attr($row->email) . "</td>" . "<td>" . $row->firstname . "
</td><td>" . $row->lastname . "</td>" . "<td>" . $row->signuptime . "</td></tr>";
//fetching data from user_ip field
}
In your SQL-statement on your first row: SELECT * FROM user , all fields are returned into the $results array of objects. If you want to specify which fields are returned you simply include them instead of the *, e.g. SELECT id, email, firstname, lastname FROM user
I have a table which is full with data from my database. I am now trying to implement a delete feature on that table, I do have the correct syntax to echo checkboxes but I don't know how to format the line correctly so the corresponding checkbox aligns correctly with each row. Any ideas? (I currently have the line echo'd above the closing table tag. Which makes the boxes appear above the table)
$sql = "SELECT * FROM products";
$answer = mysqli_query($connection, $sql);
if ($answer->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Name</th><th>Description</th><th>Price</th><th>Stock</th><th>Delete Product</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["product_id"]
echo "0 results";
}
Insert the checkbox in an additional table cell (td).
A <tr> element contains one or more <th> or <td> elements. Another content is not treated as part of that row.
echo "<tr><td>" . $row["product_id"]
. "</td><td>" . $row["product_name"]
. "</td><td> " . $row["product_description"]
. "</td><td> " . $row["product_price"]
. "</td><td> " . $row["product_cost_price"]
. "</td><td> " . $row["product_stock"]
. "</td><td>" . $row["product_ean"]
. "</td><td>" .'<input name="delete['.$row['product_id'].']" type="checkbox">'
. "</td></tr>";
I am trying to be able to access multiple dates and be able to map them to one player, and one country. The multiple years are in one table on the database, this table then uses foreign keys to attach the other two tables (player, country). How can access all of the years that the one individual won without adding it as a separate table?
below is my sql and php.
Many thanks in advance
$sql = "SELECT w.year, p.wikilink, c.countryname, c.countrylink, c.region, c.regionlink, p.playername, c.flag
FROM worldchampionperiod AS w
LEFT JOIN country AS c
ON w.Country_idcountry = c.idcountry
LEFT JOIN player AS p
ON w.player_idplayer = p.idplayer
GROUP BY p.idplayer
ORDER BY year";
$result = $conn->query($sql);
if ($result->rowCount() > 0) {
// output data of each row
while($row = $result->fetch()) {
echo "<tr>";
// echo "<td>" <a href=$row[""]>delete</a>. $row["playername"] . "</td>";
echo "<td>" . $row["playername"] . "</td>";
echo "<td>" . $row["year"] . "</td>";
//echo "<td>" . $row["countryname"] . <a href="'. $row["countrylink"] .'"> . "<br>" . $row["region"] . '<a href="'. $row["regionlink"] .'">' "</td>";
echo "<td>". ''. $row["countryname"].'' ."<br>" . ''. $row["region"].'' . "<br>" . '<img src="' .$row["flag"].'"width=30px>' . "</td>"; "</td>";
echo "</tr>";
I'm designing a website for a neighbor for a potential restaurant he wants to open. I need to create a page for testimonials/review. I'd like to code it so that the number of stars is in a SQL, I'm just not sure how to do it.
Essentially what I'm looking for is a way to determine the integer of the ratings field (1-5) and for each number, display a star image.
So:
if rating = 1, display star.png once
if rating = 2, display star.png twice
...
if rating = 5, display star.png five times
I'm just not sure how to write the code to do so.
The website is being written in PHP and CSS. I'd like to avoid JQuery, Javascript, .NET, and so forth, as I'm not as familiar with them and will be the one keeping the site up to date.
Here's what I've got so far, but it's not working right, and I get a syntax error:
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result))
{
IF (Rating = "1"()){
echo '<img src="star.png">' . }
ELSE IF (Rating = "2"()){
echo '<img src="star.png"><img src="images/star.png">' . }
Else IF (Rating = "3"()){
echo '<img src="star.png">star.png"><img src="images/star.png">' . }
ELSE IF (Rating = "4"()){
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png">' . }
ELSE
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png"><img src="images/star.png">' .
"<br/> <b>" .
$row['Name'] .
"</b> <em>" .
$row['City'] . $row['State'] . $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>"
}
?>
Use a select statement to get the ratings for a place from your database.
Store the result in a php variable (lets call it $rating)
Use php logic to output number of stars (in html obviously) based on value of $rating.
Hope that helps :)
I would recommend 3 tables for this idea.
Users Table
UserRatings Table
Dish Table
Users table is used to store just that. User information. Possibly a username, password, first name, last name for example. The table should have a primary key. Call it UsersID. It should auto increment itself and be unique for every row.
The Dish table is next. Put a dish name in it. It should have a primary key as well. Call it DishID.
Lastly is the UserRatings table will store UserRatingsId, Rating, InsertTimeStamp, UpdateTimeStamp.
Use a loop to output your HTML based on your rating.
$rating = 4; //Figure this out in your script and set accordingly
for($i = 0; $i < $rating; $i++) {
echo '<img src="star.png" />';
}
Should print out four stars for you.
Help from a friend:
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$starCount = 0;
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result)) {
$starCount = $row['Rating'] ;
while ($starCount > 0) {
echo '<img src="images/star.png">';
$starCount -- ;
}
$starCount = 0;
echo "<br/> <b>" . $row['Name'] . "</b> - <em>" .
$row['City'] .", ". $row['State'] ." ". $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>" ;
}
?>
$number=$row->rating ;
$middle="";
$first="<td width='200' align='left'>";
for($x=1;$x<=$number;$x++) {
$middle=$middle.img($fullimage_properties);
}
if (strpos($number,'.')) {
$middle=$middle.img($halfimage_properties);
$x++;
}
while ($x<=5) {
$middle=$middle.img($blankimage_properties); ;
$x++;
}
echo $last=$first.$middle."</td>";