I have a div relatedListings that contains a table of customer's orders.
I want to add a heading above it, I have tried h3, div, and span but it always puts the element below the table.
The code that generates the table:
echo '<div id="relatedListings">
<h3 id="relatedListingsHeader">Customer\'s Listings</h3>' .
$account->listAds() .
'</div>';
The actual source from the browser after executed:
<table id='customerAds'>
<!-- Generated table data -->
</table><div id="relatedListings">
<h3 id="relatedListingsHeader">Customer's Listings</h3></div>
Notice in the second snippet, the div is AFTER the table. Any ideas?
EDIT: here is the listAds method:
echo "<table id='customerAds'>
<tr><th>Ad</th><th>Title</th><th>Description</th><th>Ad Date</th><th>Actions</th></tr>";
while ($ad = $ads->fetch()) {
echo "<tr id='row_{$ad['idAds']}'>
<td>{$ad['idAds']}</td>
<td>{$ad['Title']}</td>
<td>" . substr(strip_tags($ad['Description']), 0, 100) . "...</td>
<td>" . format::dateConvert($ad['DatePosted'], 12) . "</td>
<td><a href='#'>Delete</a></td>
</tr>";
}
echo "</table>";
I would venture to guess that $account->listAds() is echo-ing as well and not returning instead. If you can change that function to return instead you should be better off.
Update:
Now having seen your updated code - I'd suggestion having that function return a string - instead of using echo like you are
$tableCode = "<table id='customerAds'>
<tr><th>Ad</th><th>Title</th><th>Description</th><th>Ad Date</th><th>Actions</th></tr>";
while ($ad = $ads->fetch()) {
$tableCode .= "<tr id='row_{$ad['idAds']}'>
<td>{$ad['idAds']}</td>
<td>{$ad['Title']}</td>
<td>" . substr(strip_tags($ad['Description']), 0, 100) . "...</td>
<td>" . format::dateConvert($ad['DatePosted'], 12) . "</td>
<td><a href='#'>Delete</a></td>
</tr>";
}
$tableCode .= "</table>";
return $tableCode;
Your generated table data may be malformed, causing the browser to try and fudge the markup into what it thinks you meant. Since you didn't include that markup, I couldn't tell for sure. It would also be helpful to see the code from $account->listAds().
Related
I am displaying proclamations in a table and I want to make each row clickable which I kinda did with js onclick function (i can only click on the name but i want to apply this to whole row). Now I want to see details about proclamations in new tab. My question starts here. I want to get proclamation ID when I click on a row but no idea how to do it. Plus i am doing onclick with js and rest of it is php so it's getting kinda messy there. I know it's not the best way to do it so i need your advice about how can I solve this issue.
P.S. I am not very familiar with js, those js parts been taken from web tutorials. And the code below is the section where I display the search results.
<section id="results">
<div class="container">
<table>
<tr>
<th>Name</th>
<th>Where</th>
<th>From</th>
<th>Date</th>
<th>Price</th>
<th>Type</th>
<th>PId</th>
</tr>
<?php
if(isset($_POST['search_btn'])){
include_once "db_connect.php";
$from = $_POST['from'];
$where = $_POST['where'];
$date = $_POST['date'];
$type = $_POST['type'];
$procid = $_POST['proc_id'];
if(empty($from) || empty($where) || empty($date) || empty($type)){
header("Location: index.php?search=empty");
exit();
}else{
$sql = "SELECT * FROM proc WHERE p_from = '".$from."' and p_where = '".$where."' and type= '".$type."' ";
$query = mysqli_query($conn, $sql);
$num_rows = mysqli_num_rows($query);
while ($rows = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
echo "<tr><td onclick='content(this)'>" . $rows['p_name'] . " " . $rows['p_surname']. " </td><td>" .$rows['p_from']. "</td><td>" .$rows['p_where']. "</td><td>" .$rows['p_date']. "</td><td>" .$rows['price']. "</td><td>" .$rows['type']. "</td></tr>" ;
}
echo "</table>";
}
}else{
echo "Error!";
}
?>
<?php
$fullUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(strpos($fullUrl, "search=empty") == true){
echo "<p class='error'>You did not fill in all fields!</p>";
}
?>
</table>
</div>
</section>
<script>
function content(elem){
<?php
?>
window.open('procdetail.php');
}
</script>
In your JavaScript function that calls window.open, you need to call the second parameter which will be the target to open the window, adding _blank will open it on a new tab instead of the same window:
window.open('procdetail.php', '_blank');
Give JavaScript the data needed to handle your information.
Also, if your idea is to open the tab with JavaScript with the content specially for the row, then you should try returning the necessary information to it, for example, if you want to open a new window with the content of a user, then you must let know JavaScript which user id should open.
This is as easy as returning in an attribute for your row element the value of the user id which data is being outputted by PHP.
Do something like this in your inline PHP script:
echo "
<tr class='table-item' data-userid='USER_ID_VARIABLE_HERE'>
<td>" . $rows['p_name'] . " " . $rows['p_surname']. " </td>
<td>" .$rows['p_from']. "</td>
<td>" .$rows['p_where']. "</td>
<td>" .$rows['p_date']. "</td>
<td>" .$rows['price']. "</td>
<td>" .$rows['type']. "</td>
</tr>
";
And then in your JavaScript code you could use an event listener for every element in your DOM with class="table-item":
window.onload = () => {
let tableItems = document.querySelectorAll(".table-item");
tableItems.forEach((element) => {
element.addEventListener("click", (event) => {
//Handle your click event here
let userId = event.target.dataset.userid; //Your user id
window.open('procdetail.php?id=' + userId, '_blank');
});
})
}
This will open in a new tab the contents of the url given which is procdetail.php?id=userId. So then, in your procdetail.php file you must handle the $_GET["id"] variable to show the respective data.
Additional resources:
You can read more of the window.open method here.
Try this
link name
If you want to continue to use inline JavaScript, you will first need to change the onClick attribute to be on the <tr> element instead of the <td> element. Once that is done, you can use the same function you have to pass the proc_id to the URL in the window.open(); method you have set in the function.
HTML/PHP
echo '<tr onclick="content(\''. $rows['proc_id'] .'\')">';
Javascript
function content(ID){
window.open('procdetail.php?proc_id=' + ID, '_blank');
}
This will make each of the table rows a clickable element that links to procdetail.php?proc_id=<YOUR_ID>
There is a way to perform this in HTML as well, but you want to modify the CSS of the table elements so that there are not non-clickable dead zones in the table row.
Using your current method of the single line echo, you could use something similar to the code below. This will append the proc_id data as a URL parameter for procdetail.php to parse.
Notice that the code only includes p_name and p_surname. You would have to add each definition with the same method.
PHP
echo "<tr><td><a href='procdetail.php?proc_id=". $rows['proc_id'] ."' target='_blank'>". $rows['p_name'] . "</a></td><td><a href='procdetail.php?proc_id=". $rows['proc_id'] ."' target='_blank'>". $rows['p_surname'] ."</a></td></tr>";
CSS
table tr td a {
display:block;
height:100%;
width:100%;
}
table tr td {
padding-left: 0;
padding-right: 0;
}
Check out this thread on the issue. It includes a usable demo that is hosted in JSFiddle.
I have an associative array of 20 names in this format:
$operationCodeName = array(
"Overlord" => 44,
"Rolling Thunder" => 68,
"Desert Storm" => 91,
"Phantom Fury" => 04,
...);
...and used the following foreach loop to generate a table (nested in a form) that displays each name and an integer year:
foreach($operationCodeName as $operation => $year) {
echo "<tr>
<td>" . $operation . "</td>
<td align='center'>" . $year . "</td>";
echo "<td><input name='comment" . $operation . "' size='50' type='text' placeholder='Comment here'></td>
</tr>";
}
I named the variables by concatenating the word 'comment' with the $operation variable in the name attribute of the text input.
This part is working properly, although if there's a smarter way to do this, I'm all ears!
When the user clicks the 'Submit' button, I need those comments to be displayed on a summary page. The table should display the same $operation and $year array elements just as on the first page, but then display the user's comments from that previous page.
I tried using a two-part solution to solve the problem:
First, my idea was to use the previous associative array to create each concatenated variable, use it to call the _POST method:
foreach($operationCodeName as $key => $value){
${"comment" . $key} = $_POST[${"comment" . $key}];
}
...and then loop through each in the comment column by echoing the concatenated variable, just as I had done to set them on the previous page in the third column:
foreach($operationCodeName as $operation => $year) {
echo "<tr>
<td>" . $operation . "</td>
<td align='center'>" . $year . "</td>";
echo "<td>" . ${"comment" . $operation} . "</td>
</tr>";
}
When I run this code, the table displays the first two columns correctly but generates empty <td></td> tags for the third column. var_dump(_$POST); displays:
array(21) { ["commentOverlord"]=> string(9) "Comment 1" ...
"Comment 1" was the comment entered in the previous page, so the data is here and I'm just not properly calling it for display in the table... That's actually encouraging!
I am assuming that I'm not properly declaring the variables, but I'm not sure where my miscommunication is happening.
How can I properly retrieve the comments from the first page, and display them in the third column of this dynamically generated table?
Thanks for any and all suggestions! While I've been programming with Java for about a year, I've only been using PHP for about 2 weeks. That said, please forgive me for any rookie mistakes!
In your code above, you tried to create dynamic variables for each of the comment. That's not how dynamic variables can be created. You already have the data in POST request in array form so you can access each comment by the key you have created in the form comment concatenated with operationCode number. Try this!
foreach($operationCodeName as $operation => $year) {
// replace space in operation key with _
$operation = preg_replace('/\s+/', '_', $operation);
$commentKey = "comment" . $operation;
$comment = $_POST[$commentKey];
echo "<tr>
<td>" . $operation . "</td>
<td align='center'>" . $year . "</td>";
echo "<td>" . $comment . "</td>
</tr>";
}
Today i have been trying to get all the records from my database, but while doing it i have had some issues.
When i'm trying to get all the records from the database, i get some complete registers but some times, from a string of 3 characters i only get 1 character.
Here goes the photo from the database where i have the records and then the photo from the view of the webpage:
View of all the rehister in the MySQL Database:
View of the registers in the webpage
Here comes the PHP code:
<?php
include("connections.php");
$query = "SELECT * FROM Encrypt";
$do_query = mysqli_query($connection, $query);
if (mysqli_num_rows($do_query)) {
while ($row = mysqli_fetch_assoc($do_query)) {
echo
"
<tr>
<td>" . $row["id"] . "</td>
<td>" . $row["name"] . "</td>
<td>" . $row["content"] . "</td>
<td>" . $row["date"] . "</td>
</tr>
";
}
}
?>
The problem is that part of your ouput is seen as html by the browser. The opening of an html tag for example: <.
You need to encode the strings correctly so that there are no characters that might cause the browser to treat it as html:
For example:
...
"<td>" . htmlspecialchars($row["content"], ENT_QUOTES) . "</td>"
...
I'm struggling with a strange error.
I create an HTML table from a mysql query using PHP like so:
echo "<div id=\"table_div\">";
echo "<table id=\"ad_table\">";
echo "<tr id=\"table_header\"><td>Media Id</td><td> Company</td><td>Title/Claim</td><td>Start - End</td><td>Website/Email</td><td></td></\td></tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr id= " . $row['Id']. "><td><b>" .$row['Titel'] . "</b> <br>" . $row['Offer'] . "</td><td><button class=\"del_btn\" rel=" . $id . ">Delete</button> </td></\td></tr>";
}
echo "</table>";
echo "</div>";
But the table is not visible. I can inspect the HTML and table and all expected rows are present but the table class has the attribute display: none. The style is not coming from my stylesheet (I tried it without providing any). I don't know why the table is hidden. Happens in both Chrome and Firefox.
Any help is greatly appreciated.
This is caused by AdBlock browser plugin
I'm trying to make automatically generated pages for users based on the username.
Basically I'm creating a table in HTML which displays all the users currently registered. The names are hyperlinks which redirect to a page in which I will post a welcome message.
The problem is that I don't know how to pass the name of the user I click on to the next page.
<table border='1'>
<tr>
<th>Username</th>
<th>Email</th>
</tr>
<?php
while($currentRow = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $currentRow['name'] . "</td>";
echo "<td>" . $currentRow['email'] . "</td>";
echo "</tr>";
}
?>
As you can see, I tried using get, but it won't work, I think it's because it doesn't know what I'm clicking on. The point is that I want the page with the welcome message say something like "welcome, $username". Any ideas?
You are closing the string with single quotes "'" that is why $_GET does not work. Replace profile.php?name='" with profile.php?name=" and also remove the single quote on the other end.
You have need remove single quote after ?name= and add it to before character >
Use
echo "<td><a href='/templates/profile.php?name=". $currentRow['name'] ."'>" .
$currentRow['name'] . "</a></td>";
Instead of
echo "<td><a href='/templates/profile.php?name='". $currentRow['name'] .">" .
$currentRow['name'] . "</a></td>";
try this in profile.php
Welcome <?php echo $_GET['name'];?>
In your profile.php
echo $_GET['name'];
echo "<td><a href='/templates/profile.php?name=". $currentRow['name'] ."'>" . $currentRow['name'] . "</a></td>";
close the single code after name value you are sending in a tag..
thats it.