php variable into a html hyperlink - php

I have looked at all the posts I could find but they don't quite to seem to do it!
Any help appreciated.
Have inserted relevant php code here:
Previously having done the search and placed the number of matches in the variable $num_rows I then turn the relevant data into variables $displayurl and $displaytitle, then place them in a table cell. I want to display the data in $displayurl as a hyperlink.
$i = 0;
while ($i < $num_rows) {
$displayurl=mysql_result($result,$i,"url");
$displaytitle = mysql_result($result, $i, "pagetitle");
echo "<tr>";
echo "<td align = left>" .$displayurl. " </td>";
echo "<td align = left> " .$displaytitle. " </td>";
echo "</tr>";
$i++;
}

echo "<td align = left><a href='" .$displayurl. "'>link1</a></td>";

surround it by <A> tag
$i = 0;
while ($i < $num_rows) {
$displayurl=mysql_result($result,$i,"url");
$displaytitle = mysql_result($result, $i, "pagetitle");
echo "<tr>";
echo "<td align = left><a href='" .$displayurl. "'>link1</a> </td>";
echo "<td align = left> " .$displaytitle. " </td>";
echo "</tr>";
$i++;
}

You can also do it this way
echo "<td align = left><a href='$displayurl'>Click here</a></td>";
OR
echo "<td align = left>Click here</td>";

In the HTML, an anchor tag or link is written as Text that gets the underline
<?php
$i = 0;
while ($i < $num_rows) {
$displayurl = mysql_result($result, $i, 'url');
$displaytitle = mysql_result($result, $i, 'pagetitle');
echo '<tr>';
echo '<td align="left"> ' . $displayurl . ' </td>';
echo '<td align="left"> ' . $displaytitle . ' </td>';
echo '</tr>';
$i++;
}

Related

How to stop a ++$i auto increment after a certain number

I'm making this table for a contest, as you can see I'm fetching data from a database as from a array, the thing is there's only 3 prizes and like 40 participants, I want to show every participant and the prizes he's winning so far, but after the third position I want to show the "No Aplica" status next to the name, the thing is I can't stop the counter from going up nor I can make it a set number so it stays on the "No Aplica" option
<table style="display:inline;" class='table-personal table-striped'>
<thead>
<tr>
<th class="line-header">PosiciĆ³n</th>
<th>Premio</th>
<th>Nombre</th>
<th>Puntos Jul</th>
<th>Puntos Ago</th>
<th>Puntos Sep</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
$e = -1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
echo "<tr class='posicion'>";
echo "<td class='line-rows'>" . $premio[++$i] . "</td>";
echo "<td>" . $row["nombre"] . "</td>";
echo "<td>" . $row["puntos_julio"] . "</td>";
echo "<td>" . $row["puntos_agosto"] . "</td>";
echo "<td>" . $row["puntos_septiembre"] . "</td>";
echo "</tr>";
if ($i >= 3) {
$i = -1;
}
}
?>
</tbody>
</table>
You need to increase the value $i if the value of $i is less than 3. Otherwise no need to increase the value of $i.
Do like this:
<?php
$i = 0;
$e = -1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
echo "<tr class='posicion'>";
echo "<td class='line-rows'>" . $premio[$i] . "</td>";
echo "<td>" . $row["nombre"] . "</td>";
echo "<td>" . $row["puntos_julio"] . "</td>";
echo "<td>" . $row["puntos_agosto"] . "</td>";
echo "<td>" . $row["puntos_septiembre"] . "</td>";
echo "</tr>";
if ($i < 3) {
$i++;
}
}
?>
You can try this
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
echo "<tr class='posicion'>";
if ($i < 3) {
echo "<td class='line-rows'>" . $premio[$i] . "</td>";
} else {
echo "<td class='line-rows'>" . $premio[3] . "</td>";
}
echo "<td>" . $row["nombre"] . "</td>";
echo "<td>" . $row["puntos_julio"] . "</td>";
echo "<td>" . $row["puntos_agosto"] . "</td>";
echo "<td>" . $row["puntos_septiembre"] . "</td>";
echo "</tr>";
$i++;
}

assignment within an echo statement

In my PHP file, I have an echo statement, that outputs some HTML, within which i want to do some assignment based on onclick event.
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='". $_SESSION['dakno'] = $r[$j]; ."' >".$r[$j]."</a></td>";
I have tried a lot of combinations, but still getting syntax error because of the onclick section.
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='"<?php $_SESSION['dakno'] = $r[$j]; ?> "' >".$r[$j]."</a></td>";
EDITS:
I am an output field in a table to be a hyperlink. On clicking the link, the value of the clicked item is passed to another PHP file using a SESSION variable.
$sno = 1;
while($r = mysqli_fetch_array($rs)){
echo "<tr>";
echo "<td style='padding:10px; text-align:left;'>".$sno."</td>"; $sno++;
for( $j=1; $j<6; $j++){
if($j == 1){
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='". $_SESSION['dakno'] = $r[$j]; ."' >".$r[$j]."</a></td>";
continue;
}
else
echo "<td style='padding:10px; text-align:left;'>".$r[$j]."</td>";
}
echo "</tr>";
}
Please, help me to remove the syntax error I am making.
As specified in the question, I needed to pass the value to another PHP file when someone clicked the link. I did not want to use AJAX here as because I do not expect to update any content dynamically. After two hours of brainstorming, I solved my problem with an extremely basic solution.
$sno = 1;
while($r = mysqli_fetch_array($rs)){
echo "";
echo "<tr>";
echo "<td style='padding:10px; text-align:left;'>".$sno."</td>"; $sno++;
for( $j=1; $j<6; $j++){
if($j == 1){
echo "<td style='padding:10px; text-align:left;'><form action='stat.php' method='POST'> <input type='hidden' name='dakno' value='".$r[$j]."' > </input> <button class='dakbutton' type='submit'>".$r[$j]."</button></form></td>";
continue;
}
else
echo "<td style='padding:10px; text-align:left;'>".$r[$j]."</td>";
}
echo "</tr>";
echo "</form>";
}
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='" .
$_SESSION['dakno'] = $r[$j] .
"' >" .
$r[$j].
"</a></td>";
remove ; in $_SESSION['dakno'] = $r[$j];

Set or Assign ID or REL inside PHP code

I want to set or assign id / rel inside my table code, take a look on my code below this so every table td has id and rel so I can give statement and manipulate the date within the jQuery, any helps will be much more appreciated:
function printdata()
{
if(is_array($_SESSION['chart']))
{
echo "Here's your chart" . "<br>";
$max = count($_SESSION['chart']);
$th1 = "<th>" . "No" . "</th>";
$th2 = "<th>" . "Nama Barang" . "</th>";
$th3 = "<th>" . "Harga Barang" . "</th>";
$th4 = "<th>" . "Stok Barang" . "</th>";
echo "<table border=1>";
echo "<tr>";
echo $th1 ;
echo $th2;
echo $th3;
echo $th4;
echo "</tr>";
for ($indexo = 0; $indexo < $max; $indexo++) {
echo "<tr>";
echo "<td>" . $indexo."</td>";
echo "<td >" . $_SESSION['chart'][$indexo]['namabarang']."</td>";
//i want to assign td rel inside of this echo
echo "<td>" . $_SESSION['chart'][$indexo]['hargabarang']."</td>";
echo "<td>" . $_SESSION['chart'][$indexo]['stokbarang']."</td>";
echo " <td><button id=".$indexo." name=\"button\">Edit</button></td>";
echo "</tr>";
}
echo "</table>";
}else
{
echo "Chart is still Empty";
}
}
echo "<table border=1 id=\"{$some_php_variable_with_an_id_value_in_it}\">"
first your code is too much line over there, that is no need to do such as that, it can be more simple to build some output with the same result, here may be could to help ya
function printdata(){
if(is_array($_SESSION['cart'])){
echo "Here's Your Chart ".count($_SESSION['cart'])."<br />";
echo '<table><tr>th>No</th><th>Nama Barang</th><th>Harga Barang</th><th>Stok Barang</th><th>Action</th></tr>';
for($indexo =0; $indexp < count($_SESSION['cart']); $indexo++){
echo "<tr>
<td id=".$indexo.">".$indexo."</td>
<td id=".$_SESSION['chart'][$indexo]['namabarang'].">".$_SESSION['chart'][$indexo]['namabarang']."<td>
<td id=".$_SESSION['chart'][$indexo]['hargabarang'].">".$_SESSION['chart'][$indexo]['hargabarang']."<td>
<td id="$_SESSION['chart'][$indexo]['stokbarang']">".$_SESSION['chart'][$indexo]['stokbarang']."<td>
<td><button id=".$indexo." name=\"button\">Edit</button></td>
</tr>";
}
}else{
echo "Chart is still Empty";
}
}
You can Change <td id=".$indexo."> to what ever you want to, seems like the other, id or rel will be the same implementation i guest

How to make PHP html table have color mismatch between rows?

I am learning PHP as I code. From an example on w3schools it showed using PHP and msql to display database results on a html table. My questions is, I now have too many rows and I could not make them have mismatch colors between rows. I've tried adding style span and font color after <td but it doesn't take it. the entire PHP just don't work if I do so.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The output of the code above will be:
Firstname Lastname
Glenn Quagmire
Peter Griffin
http://www.w3schools.com/php/php_mysql_select.asp
Not entirely sure what you mean by color mismatch. Assuming that you mean alternating row colors I'd do the following:
$odd = false;
while (...)
{
echo '<tr class="'.($odd ? "odd" : "even").'">';
...
echo "</tr>";
$odd = !$odd;
}
Now you have the tr element being of class odd or even alternatingly, and may specify some additional background color for one of them in your CSS, e.g.:
tr.odd { background-color: rgba(0, 0, 0, 0.05); }
Replace this:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
with this:
$i = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr ". ($i % 2 == 0 ? 'style="background-color:grey;"' : '' .">";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$i++;
}
Every other row will have grey color.
Maybe you can use this approach with jquery
<script src="text/javascript">
$('#table tbody tr:odd').addClass('odd');
$('#table tbody tr:even').addClass('even');
</script>
and then add the styles
.odd { background-color: #color }
.even { background-color: #color }
$class = "even";
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class = "odd";
}
else
{
echo "<tr class='$class'>";
$class = "even";
}
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
CSS
tr.even
{
background-color:blue;//Pick your own color
}
tr.odd
{
background-color:green;
}
Here is a list of color names. If you want a more detailed color choices, click here.
Use
$flag = 0;
while($row = mysql_fetch_array($result))
{
if ($flag%2 == 1)
echo "<tr bgcolor=#123345>";
else
echo echo "<tr bgcolor=#643235>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$flag = $flag +1;
}

How to give alternating table rows different background colors using PHP

I have a table of data that is generated dynamically based on the contents stored in a mysql database.
This is how my code looks:
<table border="1">
<tr>
<th>Name</th>
<th>Description</th>
<th>URL</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM categories");
while ($row = mysql_fetch_assoc($query)) {
$catName = $row['name'];
$catDes = $row['description'];
$catUrl = $row['url'];
echo "<tr class=''>";
echo "<td>$catName</td>";
echo "<td>$catDes</td>";
echo "<td>$catUrl</td>";
echo "</tr>";
}
?>
</table>
Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
and that would be the end of that. However since the table rows are dynamically generated, how can I achieve this?
Or you could just use CSS:
table tr:nth-child(odd) {
background-color: #ccc;
}
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';
echo "<tr class='$class'>";
?>
It basically checks to see if $x is divisible evenly by 2. If it is, it is even.
P.S. if you haven't seen that style of if else query, it is called a ternary operator.
Set a variable to true/false or a number and then back again during each iteration. Or use the modulus operator such as $i%2==0 in a while loop where $i is a number and use this condition in a ternary statement or something that sets the class value of the <tr>
Easiest way to alternate row colors in PHP/HTML?
$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
<?
$color="1";
while ($line = mysql_fetch_array($result)) {
if($color==1){
echo '<tr bgcolor="">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc">';
$color="1";
}
?><td align="left" width="40"><?= $line[name] ?></td>
<?
}
?>
This is my working code !
Here is my working part !
`
$i=1;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
{
echo '<tr bgcolor="#FFFF00">';
}
else
{
echo '<tr bgcolor="#99FFCC">';
}
$i++;
echo "<td>" . $row['blah'] . "</td>";
echo "<td>" . $row['blah_blah'] . "</td>";
echo "</tr>";
}
echo "</table>";
`
This is how I did it. I declared a css class called "even" with all the styling i wanted. Then looped through the scenario. Hope it helps!
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i%2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>

Categories