PHP HTML Hyperlink Quotes Issue [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to output a list of links based upon database query result. The query works fine but I need to get the information for the links to populated by the same results. In this example I need to create the following string to pass along to my loadXMLDoc() Function..
$ridesid
$catrating
This is the actual line of code that is giving me an issue:
echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideid=$ridesid&catrating=$catrating onclick ='loadXMLDoc()'>$rides</a>";
I am pretty sure it is just an issue with the quotes but I have been searching and searching for a solution or an example of how to properly code this line but can't find anything. Can anyone help?
Thanks
Here is the complete PHP block:
<?php
$sql3 = "SELECT `Record_ID`, `Name` FROM `rides` WHERE `Rating` = $catrating ORDER BY `Name`";
$result3=mysql_query($sql3)or die(mysql_error());
//var_dump ($result3);
$num = mysql_num_rows($result3);
//WHILE ($row3 = mysql_fetch_array($result3)){
echo "<table>";
// Ride Category Heading
if ($catrating == 1)
echo "<span class='headertext'>Kiddie Rides</span><br \>";
if ($catrating == 2)
echo "<span class='headertext'>Family Rides</span><br \>";
if ($catrating == 3)
echo "<span class='headertext'>Thrill Rides</span><br \>";
// end heading
for ($i = 0; $i < $num; $i++){
$row3 = mysql_fetch_array($result3);
//var_dump($row3);
$ridesid = $row3[0];
$rides = $row3[1];
//echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."&catrating=". urlencode($catrating) ."'>$rides</a>";
echo "<tr>";
//echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideID=$ridesid&catrating=$catrating' onclick='loadXMLDoc()'>$rides</a>";
//echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideID=30&catrating=1 onclick='loadXMLDoc()'>$rides</a>";
//echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."&catrating=". urlencode($catrating) ."'>$rides</a>";
echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideid=$ridesid&catrating=$catrating onclick ='loadXMLDoc()'>$rides</a>";
echo "<br />";
echo "</tr>";
}
echo '</table>';
// }
?>

You must escape your quotes.
echo "<a href='javascript:void(0);' onmousedown='url1 =\'attraction_page.php?rideid=$ridesid&catrating=$catrating\';' onclick ='loadXMLDoc()'>$rides</a>";
I would also replace the # in your href attribute with javascript:void(0)

I would recommend to leave quotes when you want to use a var, or use the {$var} syntax
echo "<a href='#' onmousedown='url1 =\'attraction_page.php?rideid=".$ridesid."&catrating=".$catrating."\'' onclick ='loadXMLDoc()'>".$rides."</a>";

Related

php mysql remove row from button [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Why does this not work? I want to be redirected to id?1 and remove.php removes it from table. How is this done?
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages")
or die(mysql_error());
echo "<table border='1'>";
while ($row = mysql_fetch_array($result)) {
echo "<li class='list-group-item'>";
echo $row['header'];
echo "<br/>";
echo $row['description'];
echo "<br/>";
echo "<button type='button' class='btn btn-default btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ändra innehåll</button>";
echo " ";
echo "<form method='POST'><a href='delete.php?". $row['page_id']."' name='action' class='btn btn-danger btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ta bort sida</a></form>";
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
echo "</li>";
}
echo "</table>";
?>
delete.php
<?php
if (isset($_POST['1']))
{
foreach ($_POST['id'] as $page_id)
{
if (!mysql_query("DELETE FROM pages WHERE id = '$page_id'"))
{
echo mysql_error();
}
}
}
?>
Please help me. Thanks in advance.
You can not get the id in POST becuase you are using it as a Query String. You can get it as:
$page_id = intval( $_GET['id'] );
Now you can check it as:
if( $page_id > 0 ){
// your stuff.. use this id in your query
}
What I have changed:
Use $_GET (SUPER GLOBAL) instead of $_POST becuase of query string.
Side Note:
There is no need to use foreach loop becuase it's not an array.
I suggest you to use mysqli_* or PDO instead of mysql_* becuase its deprecated and not available in PHP 7.
Form - PHP Manual

Alternating column colors in HTML using PHP array script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
What´s wrong with the following code?
$cores = array ("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
foreach ($cores as $cor)
{
echo "<tr>";
echo "<td bgcolor = $cor></td>";
echo "</tr>";
}
But this code works:
$cores = array ("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
for ($i=0; $i<7; $i++)
{
echo "<tr>";
echo "<td bgcolor=$cores[$i]></td>";
echo "</tr>";
}
Besides, it is not giving the colors in columns (which is the goal), but in rows.
You are missing quotes around the variable:
foreach ($cores as $cor)
{
echo "<tr>";
echo "<td bgcolor = '$cor'></td>";
echo "</tr>";
}
Do this:
$colors = array("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
$color = $colors[array_rand($colors)];
and pass $color variable to bgcolor in td
echo "<tr>";
echo "<td bgcolor = '$color'></td>";
echo "</tr>";
and it will pick up colors randomly.
If your goal is the column, use:
<?php
$cores = array ("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
echo "<table>";
echo "<tr>";
foreach ($cores as $cor)
{
echo "<td bgcolor= \"$cor\">1</td>";
}
echo "</tr>";
echo "</table>";
?>

I can't apply css for "while" looping [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to apply css for a while looping but it returns only the first data if i use css , and without css it returns all the data normally. here is my code it's simple.
$users=mysql_query("select * from users");
while($user=mysql_fetch_array($users))
{
echo "<table>";
echo "<tr>";
echo "<td class='user'>";
echo $user['pseudo'];
echo "</tr>";
echo "</td>";
echo "</table>";
}
Your problem is position:fixed. This tells the browser to lay all tables, one over the other, at window position right:500; left:500; top:100;. So, everything is there, but you tell the browser to put each new data set into a new table, and stack it in front of or behind the previous dataset (=table).
My best guess as to what you wanted to achieve is
$users=mysql_query("select * from users");
echo "<table>";
while($user=mysql_fetch_array($users))
{
echo "<tr>";
echo "<td class='user'>";
echo $user['pseudo'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
$users=mysql_query("select * from users");
echo "<table>";
while($user=mysql_fetch_array($users))
{
echo "<tr>";
echo "<td class='user'>";
echo $user['pseudo'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
Your HTML was malformed before. The tr was closed, before the td was (<tr><td></tr></td>) which is invalid. Also it makes more sense to wrap the loop in the table, instead of declaring it in every iteration.

php sql subgroup list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a simple code which gives me error. Could someone help me to explain what would be the best solution. I am not just interested the solution rather than the explanation as I have been struggling with this type of listing. I have googled it but I could not find a dummy explanation.
Many thanks
<?php
// Run a Query
$result = mysql_query("SELECT * FROM weblinks ORDER BY yeargroup ASC");
$a=-1;
while ($row = mysql_fetch_array($result)) {
if ($row['yeargroup'] == $a) {
echo "<h2 class=\"class\">Year ".$row['yeargroup']."</h2>";
} else {
echo "<ul><li>";
echo "<img src=\"" . $row['img'] . "\"/>";
echo "".$row['webname']." - ".$row['weblink'];
echo "<div class=\"text\">".$row['content']."</div>";
echo "</li></ul>";
}
$a=$row['yeargroup'];
}
?>
fixed the typo error, thanks
This is how it should be
Year 1
Link1
Link2
Link3
Year 2
Link1
Link2
Link3
etc.
$a = null;
while ($row = mysql_fetch_array($result)) {
//check if $a is equal to the current yeargroup
//if not, output the header and open the list
if ($row['yeargroup'] <> $a) {
echo "<h2 class=\"class\">Year ".$row['yeargroup']."</h2>";
echo "<ul>";
}
echo "<li>";
echo "<img src=\"" . $row['img'] . "\"/>";
echo "".$row['webname']." - ".$row['weblink'];
echo "<div class=\"text\">".$row['content']."</div>";
echo "</li>";
//check if $a is equal to the current yeargroup
//if not, close the list and set $a to the yeargroup for the loop
if ($row['yeargroup'] <> $a) {
echo "</ul>";
$a = $row['yeargroup'];
}
}

php list loop div error [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
New to php. Trying to create a list of images with text in a loop. The script works fine with just , but I want to add a class name to it so that I can control it with css, but I am getting an error. Seems not to flow cleanly with HTML. Any suggestions?
$rs = $conn->Execute("SELECT ProductName, ProductID FROM Products ");
//opens a recordset from the connection object
while (!$rs->EOF) {
$fv1=$rs->Fields("ProductID");
$fv2=$rs->Fields("ProductName");
print "<div class="product-img"> ";
print "<a href='moredetails.php?productid=$fv1'>";
print "<img src = \"thumbs/artifact-$fv1.jpg\" alt = \"Artifact-$fv1\"</a><br>";
print "<a href='moredetails.php?productid=$fv1'>";
print "$fv2</a><br>";
print "</div>";
$rs->MoveNext();
}
$rs->Close();
The double quotation will close the php function, try to use a single quotation for your class attr.
print "<div class='product-img'> ";
or escape it
print "<div class=\"product-img\"> ";
Change
print "<div class="product-img"> ";
to
print "<div class=\"product-img\"> ";

Categories