PHP active page code - I can't figure out parse error - php

I'm trying to build an active page menu with PHP and MySQL and am having a difficult time fixing the error. In the while statement I have an if statement that is giving me fits. Basically I think I'm saying that "thispage" is equal to the "title" based on pageID and as the menu is looped through if "thispage" is equal to "title" then echo id="active".
Thanks
<?php
mysql_select_db($database_db_connection, $db_connection);
$query_rsDaTa = "SELECT * FROM pages WHERE pagesID = 4";
$rsDaTa = mysql_query($query_rsDaTa, $db_connection) or die(mysql_error());
$row_rsDaTa = mysql_fetch_assoc($rsDaTa);
$totalRows_rsDaTa = mysql_num_rows($rsDaTa);
$query_rsMenu = "SELECT * FROM menu WHERE online = 1 ORDER BY menuPos ASC";
$rsMenu = mysql_query($query_rsMenu, $db_connection) or die(mysql_error());
$thisPage = ($row_rsDaTa['title']);
?>
<link href="../css/MainStyle.css" rel="stylesheet" type="text/css" />
<h2><?php echo $thisPage; ?></h2>
<div id="footcontainer">
<ul id="footlist">
<?php
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
echo (" <li" . <?php if ($thisPage==$row_rsDaTa['title']) echo id="active"; ?> . "" . $row_rsMenu['menuName'] . "</li>\n");
}
echo "</ul>\n";
?>
</div>
<?php
mysql_free_result($rsMenu);
mysql_free_result($rsDaTa);
?>

kind of a big, hairy line. i think you need to make it a little easier by splitting it into more than one line. also, what is this part of your line supposed to do?
echo id="active";
do you mean
echo " id=\"active\" ";
note i added a space before "id" because you don't have one after the LI

Parse errors can be located by consecutive removing various blocks of code.
Remove some portions of your code and see, if error persists.
Say, you can temporarily remove html part. If error got eliminated - it's in this part. Now you can divide this part on smaller blocks and so on. Thus you can locate an erroneous line pretty close.
Also, the error message usually contains some vital information on the error.

First, you have <?php nested inside another <?php. This causes:
syntax error, unexpected '<'
Let's remove both <?php and ?>. Now I see that you want you output the id, but you don't tell PHP that it's a string. Wrap it in single quotes so that echo id="active"; becomes echo ' id="active"';
With this out of the way, you can't contatenate an if statement just like that:
echo (" <li" . if ($thisPage==$row_rsDaTa['title']) echo ' id="active"'; ...
You might want to introduce a variable that will store the string id="active" if you're on the current page.
$id = '';
if ($thisPage==$row_rsDaTa['title']) {
$id = ' id="active"';
}
This piece of code might look like this when rewritten:
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
$active = '';
if ($thisPage==$row_rsDaTa['title']) {
$id = ' id="active"';
}
echo (" <li" . $id . "<a href=\"../" . $row_rsMenu['menuURL'] . "\">" .
$row_rsMenu['menuName'] . "</a></li>\n");
}
echo "</ul>\n";

Try this :
<?php
mysql_select_db($database_db_connection, $db_connection);
$query_rsDaTa = "SELECT * FROM pages WHERE pagesID = 4";
$rsDaTa = mysql_query($query_rsDaTa, $db_connection) or die(mysql_error());
$row_rsDaTa = mysql_fetch_assoc($rsDaTa);
$totalRows_rsDaTa = mysql_num_rows($rsDaTa);
$query_rsMenu = "SELECT * FROM menu WHERE online = 1 ORDER BY menuPos ASC";
$rsMenu = mysql_query($query_rsMenu, $db_connection) or die(mysql_error());
$thisPage = ($row_rsDaTa['title']);
?>
<link href="../css/MainStyle.css" rel="stylesheet" type="text/css" />
<h2><?php echo $thisPage; ?></h2>
<div id="footcontainer">
<ul id="footlist">
<?php
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
$id = ($thisPage==$row_rsDaTa['title']) ? "id='active'" : "";
echo "<li " . $id . "<a href='../" . $row_rsMenu['menuURL'] . "' >" . $row_rsMenu['menuName'] . "</a></li>\n";
}
?>
</ul>
</div>
<?php
mysql_free_result($rsMenu);
mysql_free_result($rsDaTa);
?>

Related

How can i post data from a select menu populated from database in to a table?

My php form here i made a drop down menu populated from a database...
<?php
include("mysql_connect.php")
?>
<html>
<head>
<link rel="stylesheet" href="form-style.css">
</head>
<body>
<form action="form_result.php" method="post">
<select name="cpu">
<?php
$result = $conn->query("SELECT * FROM cpus");
while ($row = $result->fetch_assoc()) {
echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";
}
?>
</select>
Here i tried to post the selected options from the drop down menu, but the results were just showing the ID number of the database table so i tried to make a function that would echo the Name of the product for that specific ID, but it didn't work...
<?php
include("mysql_connect.php");
function select_cpu() {
$result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
while ($row = $result->fetch_assoc()) {
echo "{$row['CpuManufacturer']} {$row['CpuName']}";
}
}
if(isset($_POST['submit'])) {
$cpu = $_POST['cpu'];
echo "<table>";
echo "<tr><th>You have selected:</th><tr>";
echo "
<tr><td hidden>".$cpu."</td><td>".select_cpu()."</td></tr>
";
}
echo "</table>";
?>
In the select_cpu function, you should return the string instead of echoing it:
function select_cpu() {
$result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
$str = "";
while ($row = $result->fetch_assoc()) {
$str .= "{$row['CpuManufacturer']} {$row['CpuName']}";
}
return $str;
}
Your first code snippet was almost right, but if you viewed the source you'd notice the <option> code was wrong. This is because you're mistaking the double quotes shown in HTML with what PHP sees. To output a double quote in a PHP echo, either escape it ($something = "a double quote: \"") or wrap it in single quotes instead ($something = 'a double quote: "').
Try changing this line:
echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";
to this:
echo '<option value="' . $row['CpuID'] . '">' . $row['CpuManufacturer'] . $row['CpuName'] . '</option>';

Passing value from SQL query to another SQL query in php

I want to use a variable that comes from a table i MySQL and pass it to Another SQL-Query with PHP. Can´t get it to work and I can´t find out why.
Here is the code:
<html>
<head><title></title></head>
<body>
<div>
<?php
if (isset($_GET['read_blog_posts_scrolling']))
{
$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date
FROM blog
INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID
WHERE blog.BlogID='$blogs_profile_id' // Here it is, it says undefined variable
ORDER BY blogpost.Date DESC")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<p>';
echo "Titel: " . "<strong>" . $row['Blogpost_title'] . "</strong>" . " - Bloggare " . $row['Blogwriters_name'] . " " . $row['Date'] . '<br />';
echo '<hr />';
echo '</p>';
}
}
?>
<?php
$result = mysql_query("SELECT BlogID, Blogwriters_name FROM blog")
or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$blogs_profile_id = $row['BlogID']; // I want to pass this value to above and use it in the query
echo '<p>';
echo $row['Blogwriters_name'] . '<br />';
//When clicking in this link I want the query to execute and values in BlogID to be passed
echo 'Choose blogwriter';
echo '</p>';
?>
</div>
</body>
</html>
it says the variable is undefined. How can I define it and pass the value when the a href-link is clicked?
Error is clear. Undefined variable:
You didn't defined this variable anywhere
before select statement
$blogs_profile_id
I think you need to add this variable in query string and get from $_GET.
UPDATE 1:
You have following issues in your code.
Missing blog_profile_id in your query string.
Undefined variable means you are using a variable but didn't defined.
Using mysql_* extension its deprecated
Solution:
Replace this:
echo 'Choose blogwriter';
With:
echo 'Choose blogwriter';
And than use that:
if (intval($_GET['blog_id']) > 0)
{
$blogs_profile_id = intval( $_GET['blog_id']);
$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date FROM blog INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID WHERE blog.BlogID=".$blogs_profile_id." ORDER BY blogpost.Date DESC")
or die(mysql_error());
.....
Change the order of your queries. The second query code has to be coming first in order as below
<html>
<head><title></title></head>
<body>
<div>
<?php
$result = mysql_query("SELECT BlogID, Blogwriters_name FROM blog")
or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$blogs_profile_id = $row['BlogID']; // I want to pass this value to above and use it in the query
echo '<p>';
echo $row['Blogwriters_name'] . '<br />';
//When clicking in this link I want the query to execute and values in BlogID to be passed
echo 'Choose blogwriter';
echo '</p>';
?>
<?php
if (isset($_GET['read_blog_posts_scrolling']))
{
$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date
FROM blog
INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID
WHERE blog.BlogID='"+$blogs_profile_id+"' // Here it is, it says undefined variable
ORDER BY blogpost.Date DESC")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<p>';
echo "Titel: " . "<strong>" . $row['Blogpost_title'] . "</strong>" . " - Bloggare " . $row['Blogwriters_name'] . " " . $row['Date'] . '<br />';
echo '<hr />';
echo '</p>';
}
}
?>
</div>
</body>
</html>

Mysql Field Data not displaying when a link is clicked?

I'm trying to get data from a database if a link is clicked.
I used the example codes suggested from this example -Getting mysql field data when a link is clicked?
But it doesn't work when I click on a link nothing comes up.
main.php
<?php
include('conn.php');
$sql2 = "SELECT Title FROM addpromo";
$result2 = mysql_query($sql2);
echo "<div id=\"links\">\n";
echo "<ul>\n";
while ($row2 = mysql_fetch_assoc($result2)) {
echo "<li> <a href=\"fullproject.php?title=\""
. urlencode($row2['Title']) . "\">"
. htmlentities($row2['Title']) . "</a>\n</li>";
}
echo "</ul>";
echo "</div>";
?>
This is displaying correct.but when I click at a link nothing is showing up in fullproject.php, Just a blank page.
fullproject.php
<?php
// Connect to server.
include('conn.php');
$projectname = isset($_GET['Title']);
$sql1 = "SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title'] . "<br />";
echo "<br /> ";
}
?>
Can someone help me to fix this, or any other way to make this(to get data from a database if a link is clicked) possible?
Change to this
main.php
<?php
include('conn.php');
$sql2="SELECT Title FROM addpromo";
$result2=mysql_query($sql2);
echo '<div id="links">';
echo '<ul>';
while($row2 = mysql_fetch_assoc($result2)){
echo '<li>'.htmlentities($row2['Title']).'</li>';
}
echo '</ul>';
echo '</div>';
?>
fullproject.php
<?php
if(isset($_GET['title'])){
include('conn.php');
$projectname= $_GET['title'];
$sql1="SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title']. "<br />";
echo "<br /> ";
}
}
?>
This is storing a boolean value $projectname= isset($_GET['Title']);, whether or not the title is set. Instead use $projectname = $_GET['Title'];
isset returns a boolean value (true/false) and you want the actual value of the variable:
$projectname= $_GET['title'];
Furthermore, you have to pass only the title as the URL parameter, without enclosing it within quotes. So there is an error in this line:
echo "<li> <a href=\"fullproject.php?title=" . urlencode($row2['Title']) . "\">"
Note the lack of \" after title=

How to just show few lines from a whole blog-post on a certain page?

I am making a blog in which I want to show just the summary of all posts on the home page rather than whole posts.
Below is the function that I use to get the posts on my home page.
function get_content($id = '')
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) != 0):
while ($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "by: " . $row['author'] . ", posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And then simply I use this php code to get the posts:
<?php
if (isset($_GET['id'])) :
$obj->get_content($_GET['id']);
else :
$obj->get_content();
endif;
?>
So how to get the summary only using php function or mysql as I know that we can use mysql query to get limit the words?
function sumarize($your_string, $limit_lines){
$count = 0;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $limit_lines) break;
}
}
sumarize($row['body'], 10);
will show first ten lines
1-lined:
echo '<p>'.implode("\n",array_slice(explode("\n",$row['body']),0,10)).'</p>';
instead of line with $row['body']

Place php code inside floating css container(s)?

i have a question in regards to php and css layers.
i have the following php code:
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
//while ($db_field = mysql_fetch_assoc($result)) {
//print $db_field['planet_name'] . "<BR>";
//print $db_field['location'] . "<BR>";
while($row = mysql_fetch_array( $result )){
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo "<br> ";
}
which correctly displays a word [Planet name] and a number [System] in sequence.
The above code displays the information in rows such as;
Planet Sun is located in system 35.
Planet Saturn is located in system 30.
i'm just trying to make this information display a look a little nicer. in a way so planet name shows up in the right of a background image container and system in another corner possibly colored.
....
How do i place the above code inside floating css container(s)?
Thank you.
As it seems to be a list of planets, I would use an HTML list. You can edit the css to have it look the way you want after.
echo ' <ul class="planetList"> ';
while($row = mysql_fetch_array( $result )){
echo '<li>';
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo '</li> ';
}
echo '</ul>';
In CSS
ul.planetList {
display:block;
float:right;
background-image:url('yourBackground.jpg');
background-position:left;
background-repeat:repeat-y;
/* CSS 3 Only */
background-size:{length of your text}px 100%;
}
You could also use a table instead of the list. This way, you could get a background for the planet column, and another one for the located at sytem one.
echo ' <table> ';
echo ' <tr><th>Planet</th><th>System</th></tr>';
while($row = mysql_fetch_array( $result )){
echo '<tr>';
echo '<td class="planet">' . $row['planet_name'] . '</td>';
echo '<td class="system">' . $row['location'] . '</td>';
echo '</tr> ';
}
echo '</table>';
CSS :
table>tr.planet {
background-image:url('yourBackground.jpg');
background-position:left;
}
table>tr.system {
background-color:#CCFF00;
}
Php code should be inside tags. You can always close these tags and put some htmls tags
Example:
<div class="bla"><?php
my code
?></div>
Obviusly you can do everything in php
<?php
echo '<div class="bla">';
....phpcode....
echo '</div>';
?>
Hopefully this helps
You can do something like this to keep your code clean
<?php
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
$planets = array();
while($row = mysql_fetch_array( $result )){
$planets[] = $row;
}
foreach($planets as $planet): ?>
<div class="prettyFloatyDiv">
Planet, <?php echo $planet['planet_name']?>
is located at System <?php echo $planet['location']?>
<br/>
</div>
<?php endforeach; ?>
If you have short tags enabled, you can replace every <?php echo with <?=

Categories