I am trying to make page in php/html that will display a "product's details." I have it displaying the icon and the link to the "details page," but it will not display ONLY one item on the "details page." Currently, the product's page (where the icon and the link are) will link to the "details page" just fine. However, when the page loads it loads every item in the sql database that has an 'ID.' I expected it to only take the item's id and display the page related to that item, however I received a cluster of them all... The code I have is what follows. I am currently trying to learn PHP so please if I make an error, a really bad one like something that isn't efficient. Please post the more efficient way of doing it. I am learning it the way I was taught at school.
<?php
//This page display a topic
include('admin/variable.php');
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
$dn1 = mysql_fetch_array(mysql_query('select c.id, c.name, c.description, c.price, c.quantity, c.itemID, c.imgName, c.position, (select count(t.id) from topics as t where t.parent=c.id and t.id2=1) as topics, (select count(t2.id) from topics as t2 where t2.parent=c.id and t2.id2!=1) as replies from categories as c group by c.id order by c.position asc'));
if($dn1['id']>0)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
<title><?php echo htmlentities($dn1['name'], ENT_QUOTES, 'UTF-8'); ?> - <?php echo htmlentities($dn1['itemID'], ENT_QUOTES, 'UTF-8'); ?> - KB Computers</title>
</head>
<body>
<div class="header">
<img src="<?php echo $design; ?>/images/logo.png" alt="Forum" />
</div>
<div class="Content">
<?php
?>
<h1><?php echo $dn1['name']; ?></h1>
<?php
$dn2 = mysql_query('select c.id, c.name, c.description, c.price, c.quantity, c.itemID, c.imgName, c.position, c.position, (select count(t.id) from topics as t where c.parent=c.id and t.id2=1) as topics, (select count(t2.id) from topics as t2 where t2.parent=c.id and t2.id2!=1) as replies from categories as c group by c.id WHERE c.id = ' . $id .' order by c.position asc');
//////////////////////////////////////////////////////////
while($dnn2 = mysql_fetch_array($dn2)) //This is line 30!
//////////////////////////////////////////////////////////
{
$parent = $dnn2['parent'];
if(isset($_GET['id']))
{
if($_GET['id'] == $parent){
?>
<div id="thing">
<table>
<tr>
<th><?php echo $dnn2['imgName']; ?></th>
</tr>
<br />
<tr>
<th><?php echo $dnn2['price']; ?></th>
</tr>
<br />
<tr>
<th> <?php echo $dnn2['description']; ?></th>
</tr>
<br />
<tr>
<th><?php echo $dnn2['itemID']; ?></th>
</tr>
<?php
if(isset($_SEESION['username']) and $_SESSION['username'] == $admin){?><div class="edit"><img src="<?php echo $design; ?>/images/edic.png" alt="Edit" /></div>
<?php } ?>
</table></div>
<?php
}else{echo 'The parent and id values are not the same.';}
}}
?>
</div>
</body>
</html>
<?php
}
else
{
echo '<h2>This topic doesn\'t exisc.</h2>';
}
}
else
{
echo '<h2>The ID of this topic is not defined.</h2>';
}
?>
<?php
//This page display a topic
include('admin/variable.php');
include('home.php');
if(isset($_GET['id']))
{
$dn1 = mysql_query('select
c.id,
c.name,
c.description,
c.price,
c.quantity,
c.itemID,
c.imgName,
c.parent,
c.position,
(select count(c.id) from categories as c where c.parent=c.id) as categories, (select count(c.id) from categories as c where c.parent=c.id and c.id!=0) as replies from categories as c group by c.id order by c.position asc');
if($dn1 === false){
die(mysql_error());
}
while($dnn1 = mysql_fetch_array($dn1))
{
$parent = $dnn1['parent'];
if($_GET['id'] == $parent){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="require/style.css" rel="stylesheet" title="Style" />
<title><?php echo htmlentities($dnn1['name'], ENT_QUOTES, 'UTF-8'); ?> - <?php echo htmlentities($dnn1['itemID'], ENT_QUOTES, 'UTF-8'); ?> - KB Computers</title>
</head>
<body>
<div class="Content">
<div style="background:white; height:800px;">
<div id="shopNav">
<ul>
<li class="end">Shop</li>
<li>Catagories</li>
<li>New Products</li>
<li>My Account</li>
<li class="end2">Checkout</li>
</ul>
</div>
<div id="thing">
<table><h1><?php echo $dnn1['name']; ?></h1>
<tr>
<th><?php echo '<img src="image/' . $dnn1['imgName'].'" alt="' . $dnn1['imgName']. '" width="128" height="128"'; ?></th>
</tr>
<tr>
<th><?php echo $dnn1['price']; ?></th>
</tr>
<tr>
<th class="tableDesc"> <?php echo $dnn1['description']; ?></th>
</tr>
<tr>
<th><?php echo $dnn1['itemID']; ?></th>
</tr>
<?php
if(isset($_SEESION['username']) and $_SESSION['username'] == $admin){?>
<tr>
<img src="<?php echo $design; ?>/images/edic.png" alt="Edit" />>
</tr>
<?php } ?>
<tr>
<th> </th>
</tr>
</table>
</div>
<?php include("require/rightBar.html"); ?></div><?php include("require/footer.html");
}
}
}else{echo 'That product doesnt exist!';}
?>
</div>
</body>
Related
i am completely new to this world and i am trying to get more confident with PHP and MYSQL, so i am playing with a small web application just to fetch and retrieve data with MYSql and PHP.
I created a table in HTML and my goal is to retrieve this data from a mysql table with PHP
The problem is that the data are displayed twice... Can you help me understand where is error ?
Below the code :
<?php
session_start();
include_once("database.php");
$db = $conn;
$query = " SELECT categoria FROM categoria_prodotto";
$result = $db->query($query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<title>test</title>
</head>
<body>
<section id="main-page">
<div class="link">
<span> Create a new purchase table </span>
</div>
<table>
<tr>
<td> category </td>
<td> product </td>
</tr>
<?php while ($row = mysqli_fetch_array($result)) :
foreach ($row as $temp) {
$query1 = "SELECT `nome` FROM `supermarket`.`lista_prodotto` WHERE `categoria_prodotto` = '$temp' ORDER BY `categoria_prodotto` DESC";
$result1 = $db->query($query1);
?>
<tr>
<td><?php echo $row[0]; ?> </td>
<td>
<?php while ($row1 = mysqli_fetch_array($result1)) :
echo $row1[0]; ?>
<?php endwhile;
}
endwhile; ?>
</td>
</tr>
</table>
<div class="link">
<span> Store a new product </span>
</div>
</section>
</body>
</html>
////
And here the result in browser with two row with same data duplicated each time
[here][1]
Thank you in advance for helping me to troubleshoot my problem :)
[1]: https://i.stack.imgur.com/XEBsi.jpg
You have some unnecessary loops, also if you use the object oriented version of the MySQLI API its a lot easier to read. I also change the query to use a Prepared parameterised query that you bind data to before the executing the query, much safer and protects you against SQL Injection
<?php
session_start();
include_once("database.php");
$db= $conn;
$query = "SELECT categoria FROM categoria_prodotto";
$result = $db->query($query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<title>test</title>
</head>
<body>
<section id="main-page">
<div class="link">
<span> Create a new purchase table </span>
</div>
<table>
<tr>
<td> category </td>
<td> product </td>
</tr>
<?php
while($categoria_prodotto = $result->fetch_assoc()):
$query1 = "SELECT `nome`
FROM `supermarket`.`lista_prodotto`
WHERE `categoria_prodotto` = ?
ORDER BY `categoria_prodotto` DESC";
$stmt1 = $db->prepare($query1);
$stmt1->bind_param('s', $categoria_prodotto['categoria']);
$stmt->execute();
$result1= $stmt->get_result();
$supermarket = $result1->fetch_assoc();
?>
<tr>
<td><?php echo $categoria_prodotto['categoria']; ?></td>
<td><?php echo $supermarket['nome']; ?></td>
?>
<?php
endwhile;
?>
</td>
</tr>
</table>
</section>
</body>
</html>
So I got these issues when I tried to extract data from the database. My DB connection is working fine. It is showing "Database connection established" and inside my index.php I wrote a for each loop to get the data and inside my HTML code, I display it inside the table. I got these errors:
Notice: Undefined variable: jokes in C:\xampp\htdocs\comp1321_database\jokes\jokes.html.php on line 16
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\comp1321_database\jokes\jokes.html.php on line 16
Here is the HTML and php code to display the data:
<?php include_once 'admin/includes/helpers.inc.php';?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List of Jokes</title>
</head>
<body>
<p>Add your own joke</p>
<p>Here are all the jokes in the database</p>
<!-- into a table -->
<table border="1">
<?php foreach ($jokes as $joke): ?>
<!-- <form action="?deletejoke" method="post"> -->
<tr>
<td><?php html($joke['joketext']);?></td>
<td><?php $display_date = date("D d M Y", strtotime($joke['jokedate']));
html($display_date); ?>
</td>
<td><img height="100px" src="images/<?php html($joke['image']);?>"
/></td>
<td><input type="hidden" name="id" value="<?php echo $joke['id'];
?>">
<input type="submit" value="Delete"></td>
</tr>
<!-- </form> -->
<?php endforeach; ?>
</table>
<?php include 'admin/includes/footer.inc.html.php';?>
</body>
</html>
and here is the index.php:
<?php
// selection block
include 'admin/includes/db.inc.php';
//
try
{
$sql = 'SELECT * FROM joke';
$result = $pdo->query($sql);
} catch (PDOException $e) {
$error = 'Error fetching jokes' . $e->getMessage();
include 'error.html.php';
exit();
}
foreach ($result as $row) {
$jokes[] = array(
'joketext'=> $row ['joketext'],
'jokedate'=> $row['joketext'],
'image'=> $row['image']
);
}
include 'jokes.html.php';
?>
Many thanks.
Initialize your jokes variable before you try to use it, ie:
$jokes = [];
try
{
$sql = 'SELECT * FROM joke';
$result = $pdo->query($sql);
} catch (PDOException $e) {
$error = 'Error fetching jokes' . $e->getMessage();
include 'error.html.php';
exit();
}
foreach ($result as $row) {
$jokes[] = array(
'joketext'=> $row ['joketext'],
'jokedate'=> $row['joketext'],
'image'=> $row['image']
);
}
You need define $jokes on your php file, you can do it on header of file.
<?php include_once 'admin/includes/helpers.inc.php';?>
<?php
$jokes = someGetJokesFunction(); // write function to get data from database
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List of Jokes</title>
</head>
<body>
<p>Add your own joke</p>
<p>Here are all the jokes in the database</p>
<!-- into a table -->
<table border="1">
<?php foreach ($jokes as $joke): ?>
<!-- <form action="?deletejoke" method="post"> -->
<tr>
<td><?php html($joke['joketext']);?></td>
<td><?php $display_date = date("D d M Y", strtotime($joke['jokedate']));
html($display_date); ?>
</td>
<td><img height="100px" src="images/<?php html($joke['image']);?>"
/></td>
<td><input type="hidden" name="id" value="<?php echo $joke['id'];
?>">
<input type="submit" value="Delete"></td>
</tr>
<!-- </form> -->
<?php endforeach; ?>
</table>
<?php include 'admin/includes/footer.inc.html.php';?>
</body>
</html>
On the list.php page, I am passing a variable using hidden input method in form.
This form redirects to listd.php where the variable is used to run an SQL query to delete a particular row from the database where the variable==name.
If I print the variable on listd.php before processing SQL query then it is visible but during when query is processed it gives unidentified index error.
The code in listd.php works if I take input from the user on the same page.
list.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> Sports </title>
<link rel="stylesheet" href="main.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Tangerine">
<link href="https://fonts.googleapis.com/css?family=Dancing+Script|Great+Vibes|Roboto|Barrio" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<?php
require ("config.php");
?>
<div class="row">
<div class="container-fluid">
<div class="jumbotron text-center">
<h3> List Events</h3>
<?php
session_start();
if($_SESSION['username'])
echo "<h2>Welcome ".$_SESSION['username']."!</h2>";
else
header('location:index.php');
//session_start();
if(isset($_SESSION['username']))
echo "<h3><a href='logout.php'>Logout</a></h3>";
?>
</div>
</div>
</div>
<div class="container">
<h2>Hover Rows</h2>
<p>The .table-hover class enables a hover state on table rows:</p>
<table class="table table-hover">
<thead>
<tr>
<th>Event </th>
<th>Add</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<!--
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
-->
<?php
{$result="SELECT name FROM events";
$q = mysqli_query($conn,$result) or die(mysql_error());
while ($row=mysqli_fetch_array($q))
{
echo "<tr> <td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo '<button> Add </button> <br><br>';
echo "</td>";
echo "<td>";
echo '<form action="liste.php" method="post" >';
echo '<input type="hidden" name="edit" value="';?>
<?php echo $row['name'];
echo'"> <input type="submit" value="Edit">';
echo "</form>";
echo "</td>";
echo "<td>";
echo '<form action="listd.php" method="post" >';
echo '<input type="hidden" name="del2" value="';?>
<?php echo $row['name'];
echo'"> <input type="submit" value="Delete">';
echo "</td>";
echo "</form>";
}
}
?>
<!--
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
-->
</tbody>
</table>
</div>
</body>
</html>
listd.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> Sports </title>
<link rel="stylesheet" href="main.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Tangerine">
<link href="https://fonts.googleapis.com/css?family=Dancing+Script|Great+Vibes|Roboto|Barrio" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<?php
require ("config.php");
?>
<div class="row">
<div class="container-fluid">
<div class="jumbotron text-center">
<h3> Events </h3>
<div class="row">
<div class="container-fluid text-primary text-center">
<br>
<button class="btn"> Add </button> <br><br>
<button class="btn"> Edit </button> <br><br>
<button class="btn"> Delete </button> <br><br>
<button class="btn"> View </button> <br><br>
</div>
</div>
<?php
session_start();
?>
<form action="listd.php" method="post" class="a">
<table>
<tr>
<td> Category Name </td>
<td>
<strong>
<?php echo $_POST["del2"];
$k=$_POST['del2'];
?>
</strong>
</td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="Delete" name="delete"> </td>
</tr>
</table>
</form>
<?php
$event=$k;
if(isset($_REQUEST['delete'] )&& $event )
{
$sql="DELETE FROM events WHERE name='$event'";
if(mysqli_query($conn,$sql))
{
echo "deleted Succesfully";
//header('location:listd.php');
}
else
echo "error";
}
?>
</div>
</div>
</div>
</body>
</html>
It's really not necessary to use two forms to do this, unless this is a very dangerous action. this is also the source of your problem. $k is not preserved on the second postback. Requests are stateless. When you submit the second form, the listd.php script runs again from the start, and the values assigned to variables the last time it ran. This is the normal behaviour of HTTP requests in general.
A single-form version might look like this:
list.php
<?php
//this needs to be before anything else otherwise the redirect might fail due to headers already sent.
session_start();
if(isset($_SESSION['username'])) //corrected to use isset(). Only one test for this is needed.
{
echo "<h2>Welcome ".$_SESSION['username']."!</h2>";
echo "<h3><a href='logout.php'>Logout</a></h3>";
}
else
{
header('location:index.php');
die(); //script needs to die because webcrawlers/bots will ignore the redirect header, and view the rest of the page anyway, without permission. Using die() means the rest of the page never gets output.
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> Sports </title>
<link rel="stylesheet" href="main.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Tangerine">
<link href="https://fonts.googleapis.com/css?family=Dancing+Script|Great+Vibes|Roboto|Barrio" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<?php
require ("config.php");
?>
<div class="row">
<div class="container-fluid">
<div class="jumbotron text-center">
<h3> List Events</h3>
</div>
</div>
</div>
<?php
$message = null; //will hold any extra message to the user resulting from data processing actions (such as delete)
//only run the next section if del2 has been submitted from a postback.
if(isset($_POST['del2']))
{
$event = $_POST['del2'];
$sql= "DELETE FROM events WHERE name='$event'"; //here you should use parameterised queries, I will leave it to you to look this up online, using the link I gave in the comments.
if(mysqli_query($conn,$sql))
{
$message = "Deleted succesfully";
}
else
{
$message = "error";
//here you should check mysqli_error() and log the output somewhere (not visible to the user)
}
}
?>
<div class="container">
<h2>Hover Rows</h2>
<!-- here is an example of how you can inject the confirmation message into somewhere suitable in the page. You can modify this to suit your needs. -->
<?php if ($message != null) { echo "<p><b>".$message."</b></p>"; } ?>
<p>The .table-hover class enables a hover state on table rows:</p>
<table class="table table-hover">
<thead>
<tr>
<th>Event </th>
<th>Add</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$result="SELECT name FROM events";
$q = mysqli_query($conn,$result) or die(mysqli_error()); //corrected to use mysqli_error not mysql_error
while ($row=mysqli_fetch_array($q))
{
echo "<tr> <td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo '<button> Add </button> <br><br>';
echo "</td>";
echo "<td>";
echo '<form action="liste.php" method="post" >';
echo '<input type="hidden" name="edit" value="';
echo $row['name'];
echo'"> <input type="submit" value="Edit">';
echo "</form>";
echo "</td>";
echo "<td>";
echo '<form action="list.php" method="post" >';
echo '<input type="hidden" name="del2" value="';
echo $row['name'];
echo'"> <input type="submit" value="Delete" onclick="return confirm("Are you sure?");>'; //adds a confirmation dialog box to the delete button
echo "</td>";
echo "</form>";
}
?>
</tbody>
</table>
</div>
</body>
</html>
You do not need listd.php at all.
P.S. A separate hint: I see you are copying the same HTML <head> etc material into every page script. This is not necessary. You can create two files e.g. header.php and footer.php. In header.php you can put the opening <html> and <head> tags right down to <body> and any other enclosing tags necessary for the consistent layout of all your pages. Then in footer.php you can put the equivalent closing tags. And then in all the other pages of your site you can simply write <?php require_once("header.php"); ?> as the first line and <?php require_once("footer.php"); ?> as the last line. Then any changes you need to make to the headers (e.g. new CSS files or something) are automatically applied to all the pages without tedious copy/paste actions. Although I suggested that you remove your second script here, you can do this for any other pages in your site. It's also a good general principle of code re-use which you can apply to all kinds of situations in your future code.
P.P.S. I'd also consider using an up-to-date doctype. The HTML5 doctype declaration is simply <!DOCTYPE html>. This will likely result in more consistent behaviour of the HTML and CSS in your page, and future-proofs your application against deprecation of older doctypes which are no longer in current use.
I am working on a little forum, and I want it to be easy to identify a staff member.
A the moment, a user is defined as an $admin in the config file:
<?php
/******************************************************
------------------Required Configuration---------------
Please edit the following variables so the forum can
work correctly.
******************************************************/
//We log to the DataBase
mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('forum_database');
//Forum Staff
$admin='The_Darthonian'; // For admin forum features
/******************************************************
-----------------Optional Configuration----------------
******************************************************/
//Forum Home Page
$url_home = 'index.php';
//Design Name
$design = 'default';
/******************************************************
----------------------Initialization-------------------
******************************************************/
include('init.php');
?>
I have an icon at the path of default/images/role_moderator.gif that I want to appear on a profile if a user is defined as an admin
I further have a userid variable. For example, the first account would be 1, then the second 2 and so on which are unique. Below is the profile code:
<?php
//This page display the profile of an user
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
<title>User Profile</title>
</head>
<body>
<div class="header">
<img src="<?php echo $design; ?>/images/logo.png" alt="Forum" />
</div>
<div class="content">
<?php
if(isset($_SESSION['username']))
{
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['userid'].'" and user1read="no") or (user2="'.$_SESSION['userid'].'" and user2read="no")) and id2="1"'));
$nb_new_pm = $nb_new_pm['nb_new_pm'];
?>
<div class="box">
<div class="box_left">
Home > Users > Profile
</div>
<div class="box_right">
Your messages(<?php echo $nb_new_pm; ?>) - <?php echo htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8'); ?> (Logout)
</div>
<div class="clean"></div>
</div>
<?php
}
else
{
?>
<div class="box">
<div class="box_left">
Home > Users > Profile
</div>
<div class="box_right">
Sign Up - Login
</div>
<div class="clean"></div>
</div>
<?php
}
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
$dn = mysql_query('select username, email, avatar, signup_date from users where id="'.$id.'"');
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
?>
This is the profile of "<?php echo htmlentities($dnn['username']); ?>" :
<?php
if($_SESSION['userid']==$id)
{
?>
<br /><div class="center">Edit my profile</div>
<?php
}
?>
<table style="width:500px;">
<tr>
<td><?php
if($dnn['avatar']!='')
{
echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar" style="max-width:100px;max-height:100px;" />';
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<?php
?>
<div>
<img src="default/images/no_avatar.jpg" alt="no_avatar" />
</div>
</body>
</html>
<?php
}
?></td>
<td class="left"><h1><?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?></h1>
This user joined the website on <?php echo date('Y/m/d',$dnn['signup_date']); ?></td>
</tr>
</table>
<?php
if(isset($_SESSION['username']) and $_SESSION['username']!=$dnn['username'])
{
?>
<br />Message "<?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?>"
<?php
}
}
else
{
echo 'We could not find this user anywhere. Prehaps their account was removed.';
}
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<?php
?>
<div>
<img src="/default/images/no_avatar.jpg" alt="no_avatar" />
</div>
</body>
</html>
<?php
}
?>
</div>
<div class="foot">About Us - Terms of Service</div>
</body>
</html>
How can I make it so that if a user is defined as an $admin in the config, they have the icon appear on their profile?
I have this code, that picks article content from my word press db then outputs it onto a table, using a loop. once a user select the articles they like they submit the selection to my next page call it displaypage to display the selected articles.
the selection page code looks like this.
<?php /*
Template Name: News Selector Template
*/
//Pull the data
$posts = $wpdb->get_results
("
SELECT
p1.*,
wm2.meta_value
FROM
mp_posts p1
LEFT JOIN
mp_postmeta wm1
ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
mp_postmeta wm2
ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
WHERE
p1.post_status='publish'
AND p1.post_type='post'
ORDER BY
p1.post_date DESC LIMIT 0,30
");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div class="container">
<div class="header"><img src="/sample/images/logo_main_300x100px.png" alt="logo" name="sample_logo" width="300" height="100" id="logo" style="background-color: #FFF; display:block;" />
<!-- end .header --></div>
<div class="content">
<h1>sample Top 30 Articles</h1>
<p>Select the articles you want to add to the newsletter</p>
<form action="http://sample.net/brands/sample/newsletter/" method="post">
<table width="960" border="2" summary="A collection of all the articles">
<caption>
Article Selection
</caption>
<tr>
<th scope="col">Select</th>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Cover</th>
<th scope="col">link</th>
</tr>
<?php
// if(isset($_POST['submit'])){
if (have_posts($posts)) {
// var_dump($posts); die();
foreach($posts as $post) {
//foreach($_POST['article_list'] as $post){
?>
<tr>
<th scope="row"><input type="checkbox" name="article[]" value="<?php echo $post->ID; ?>" /></th>
<td headers="article[]"><?php echo $post->post_title; ?></td>
<td headers="article[]"><?php echo $post->post_excerpt; ?></td>
<td headers="article[]"><?php echo '<img src="https://sample/wp-content/uploads/'.$post->meta_value.'" width="100px" alt="" />'?></td>
<td headers="article[]"><?php echo $post->guid; ?></td>
</tr>
<?php
}
}
?>
<p><input type="hidden" name="metas" value="<?php echo implode(",", $post); ?>"></p>
</table>
<div>
<p><input type="submit" name="Generate" value="submit"/></p>
</div>
</form>
<!---->
</div>
<!-- end .content --></div>
<div class="footer">
<p>sample Management System</p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
Iv used implode to collect the array variables of all selected articles via a check box option.
on my display page i wrote this simple code to see the output of the array.
<?php /*
Template Name: Newsletter Template
*/
$posts = explode(",", $_POST['metas']);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php echo $posts[0]->guid ?>
<?php echo $posts[0]->meta_value ?>
<?php echo $posts[0]->post_title ?>
<?php echo $posts[0]->post_excerpt ?>
</body>
</html>
Now when i submit the selected articles from the first page i'm supposed to be able to access the various array elements in my display page, but that's not the case. i get the
ERROR
Warning: implode(): Invalid arguments passed in
/home/sampled/public_html/brands/sample/wp-content/themes/newsample/page-newsselector.php
on line 164
i don't know where i went wrong with my implementation.
HELP PLEASE