Issue with using php variable in hyperlink - php

How do you use one php variable for the url in a hyperlink around another php variable? Below is my code, which is clearly not working. I want $url to be the hyperlinked text of $songTitle.
if ($result) {
$numberOfRows = $result->num_rows;
for($i=0; $i < $numberOfRows; $i++) {
$row = $result->fetch_assoc();
echo '<tr>';
echo '<td>' . $row['artistName'] . '</td>';
echo '<td>'<a href=". $row['url'] . > . $row['songTitle'] . </a> '</td>';
echo '<td>' . $row['yOR'] . '</td>';
echo '</tr>';
}
}

You are not concatenating your strings properly.
for($i=0; $i < $numberOfRows; $i++) {
$row = $result->fetch_assoc();
echo '<tr>';
echo '<td>' . $row['artistName'] . '</td>';
echo '<td><a href="' . $row['url'] . '" >' . $row['songTitle'] . '</a></td>';
echo '<td>' . $row['yOR'] . '</td>';
echo '</tr>';
}

did you copy your script from somewhere? In parts of it you seem to understand how to escape and use PHP values inside a string, but then you messed up the one line.
Anyway, this is it fixed.. you didn't have your quotes or single quotes used properly.
echo "<td><a href='". $row['url'] . "'>" . $row['songTitle'] ." </a> </td>";

Related

Using checkbox in echo as table

I have a code where I echo a table:
<?php
// connect to the database
include('core/base.php');
// get results from database
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM uitslag ORDER BY ID ASC")
or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Nummer</th><th>Naam</th><th>Telefoon</th><th>Binnen</th> <th>Adres</th> <th>Postcode</th> <th>Wijk</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo '<td>' . $row['Telefoon'] . '</td>';
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
echo '<td>Aanpassen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
The row 'Telefoon' either echos 1 or 0. How can I echo a checkbox which is checked when 1 instead of echoing the actual number?
Define input element of type checkbox.Like this.
$telefoon = ($row['Telefoon']==1)?'checked':'';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo "<td><input type='checkbox' $telefoon></td>";
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
<?php
if($row['Telefoon']==1){
echo "<td><input type='checkbox' checked></td>";
}
else {
echo "<td><input type='checkbox' ></td>";
}
?>

tables and links messing the code up

I have a problem with PHP and links, I mean, I'm trying to use <a href to link the id of a ticket and then display it to user, but the problem is that the link I get is like
http://localhost/ucp/viewticket.php?ticketid=8%3Etest%3C/a%3E%3C/td%3E%3Ctd%3EUpute%20i%20informacije%3C/td%3E%3Ctd%3E2016-08-26%2000:06:59%3C/td%3E%3C/tr%3E%3Ctr%3E%3Ctd%3E9%3C/td%3E%3Ctd%3ESale_Lesa%3C/td%3E%3Ctd%3E%3Ca%20href=
and it should only be like
http://localhost/ucp/viewticket.php?ticketid=8
This is the code:
if ($result = $con->query($query))
{
echo '
<table id="tfhover" class="tftable" border="1">
<th>ID Tiketa</th>
<th>Autor</th>
<th>Naslov</th>
<th>Kategorija</th>
<th>Datum</th>
';
while ($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Autor'] . "</td>";
echo "<td>". $row['Naslov'] ."</td>";
echo "<td>" . $row['Kategorija'] . "</td>";
echo "<td>" . $row['Datum'] . "</td>";
echo "</tr>";
}
$result->free();
}
You are missing a double quote at the end of the href link so it's including all the HTML that follows as the link too. I've fixed it below by add \" before the end of the "a" tag.
echo "<td>". $row['Naslov'] ."</td>";
When you change your Markup everything should be fine:
while ($row = $result->fetch_assoc())
{
echo '<tr>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Autor'] . '</td>';
echo '<td>'. $row['Naslov'] .'</td>';
echo '<td>' . $row['Kategorija'] . '</td>';
echo '<td>' . $row['Datum'] . '</td>';
echo '</tr>';
}
The Reason for this is, that there is a big difference between Single Quotes and Double Quotes.
Single Quotes are not parsed by PHP, so
$variable = 'hello';
echo '$variable'
will still return: $variable
While
echo "$variable"
will parse the string to: Hello
This way you would have seen, that you missed an escaped double quote.

PHP substr breaks my table

I have a table like so
echo '<table>';
echo '<tr>';
echo '<td>' . $value['title'] . '</td>';
echo '<td>' . $value['date'] . '</td>';
echo '<td>' . $value['preview'] . '</td>';
echo '<td>' . substr($value['description'], 0, 10) . '</td>';
echo '<td><img src="../images/articles/' . $value['image'] . '"></td>';
echo '<td><img src="../images/articles/' . $value['detailImage'] . '"></td>';
if($value['showDetailImage'] == 1){
$showDetailImage = 'Yes';
}else{
$showDetailImage = 'No';
}
echo '<td>' . $showDetailImage . '</td>';
echo '<td><a class="btn btn-default" href="Press.php?action=edit&id=' . $value['id'] . '">Edit</a></td>';
echo '<td><a onclick="if(!confirm(\'Are you sure you want to delete this item?\')) return false;" class="btn btn-default" href="Press.php?action=delete&id=' . $value['id'] . '">Delete</a></td>';
echo '</tr>';
echo '</table>';
notice I have substr and when I have that in there, it does not display the description and breaks the rest of my table, if I remove the substr the table is fine and everything is displayed. Why is substr breaking my table.
It's probably the contents of description that contains HTML markup that's breaking the table, use htmlspecialchars...
echo '<td>' . htmlspecialchars(substr($value['description'], 0, 10)) . '</td>';
Is that because your substr concatenates half way through some HTML element?
For example, you should use:
echo htmlentities(substr($value['description'], 0, 10));
Or if you want to strip the HTML tags:
echo strip_tags(substr($value['description'], 0, 10));

Dynamically Apend data in html table from mysql using php

I have mysql table with following columns :
no, name, oname, quantity
I have n number of records in the tables ,I want to fetch these records in an html table that dynamically increases'decreases its row length according to the number of rows in mysql database. I am trying following but its not working.can anyone help me out here
<?php
include './connection.php';
$query = "select * from orderdetails";
$result = mysql_query($query);
echo '<table border="1" style="width:600px" align=center >';
echo '<tr bgcolor="lavendar">';
echo '<td width="15%">Order No.</td>';
echo '<td>Name</td>';
echo '<td>Order</td>';
echo '<td>Quantity</td>';
echo '</tr>';
echo '</table>';
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
echo '</table>';
?>
Where you are printing out your values, there are a few errors.
In this specific code block:
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
First off all, you would want to concatinate your strings, PHP uses . for that.
For example, to concatinate "World!" to "Hello, ", to print out Hello, World!, you would use the following:
echo "Hello, " . "World!";
You are also missing a variable sign ($) in your code, when printing out the rows.
In your case, you're using echo '<td>' row['no'] '</td>';,
where it should have been echo '<td>' . $row['no'] . '</td>';
To clarify, your code should look like this:
while($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['no'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['oname'] . '</td>';
echo '<td>' . $row['quantity'] . '</td>';
echo '</tr>';
}
Relevant PHP documentation:
PHP String Concatination
Echo documentation
There's a typo-
row['no']
to
$row['no']
Similarly other variables too.

How to hyperlink a table data in a cell in php

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" '. $row['id'] .'</td>";
echo "<td>" . $row['LayarType'] . "</td>";
echo "<td>" . $row['Attribution'] . "</td>";
echo "</tr>";
}
it gives an error...
how do i give a link suggested here in bold part..
You were not concatenating the different strings with the . operator.
Either of the following will work:
echo "<td>" . ''. $row['id'] .'' . "</td>";
// or
echo '<td>'. $row['id'] .'</td>';
Your quotes are messed up in the second echo.
Also, for strings with no variables inside them, you should use single quotes instead of double quotes so that PHP doesn't have to process the contents.
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>'. $row['id'] .'</td>'; echo '<td>' . $row['LayarType'] . '</td>';
echo '<td>' . $row['Attribution'] . '</td>';
echo '</tr>';
}

Categories