I am trying to output my array to list items within my HTML. It echo out fine but I want to format the output as a list. When I surround the echo $row["item"] in a <li> <?php echo $row["item"] ?> </li> it errors out. I don't know why.
Right now it will echo out from the PHP echo functions just fine into a large block but I want to format the array output into list items which I can style using css and I can't get this to function.
Code
<?php include 'database.php' ; ?>
<?php
$result = mysqli_query($con, "SELECT * FROM shouts");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Shout IT!</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div id="container">
<header>
<h1>SHOUT IT! shoutbox</h1>
</header>
<div id="shouts">
<?php while($row = mysqli_fetch_array($result)){
echo $row["user"] , $row["message"], $row{"time"};
}
?>
<ul>
<li class="shout">test</li>
<li class="shout"><?php echo $row["user"] ?></li>
<li class="shout"></li>
</ul>
</div>
<div id="input">
<form method="post" action="process.php">
<input type="text" name="user" placeholder="Enter Your Name" />
<input type="text" name="message" placeholder="Enter A Message" />
<br>
<input class="btn" type="submit" name="submit" value="Shout it Out" />
</form>
</div>
</div>
</body>
</html>
You get an error because your <?php echo $row["item"] ?> is outside the while that gets the records from the database. What yout need is something like:
<ul>
<?php while($row = mysqli_fetch_array($result)){
?>
<li class="shout">test</li>
<li class="shout"><?=$row["user"]?></li>
<li class="shout"><?=$row["message"]?></li>
<li class="shout"><?=$row["time"]?></li>
<?php
}
?>
</ul>
instead of the :
<?php while($row = mysqli_fetch_array($result)){
echo $row["user"] , $row["message"], $row{"time"};
}
?>
<ul>
<li class="shout">test</li>
<li class="shout"><? php echo $row["user"] ?></li>
<li class="shout"></li>
</ul>
Related
I wanna pick up record and show but this error happen.
this error was not happen when I add ['] both sides of post and id in line 30.
anyway I don't no why $post['name'] and $post['content'] doesn't appear.
[table has a data by me on this program.]
please give me advice.
<?php
include'php.php';
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>board</title>
</head>
<body>
<form action="#" method="post">
<?php if(count($errors)): ?>
<ul>
<?php foreach($errors as $error): ?>
<li>
<?php echo htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<p>name</p>
<input type="text" name="name">
<p>comment</p>
<textarea type="textarea" name="content"></textarea>
<br>
<input type="submit">
</form>
<?php
$sql = "SELECT * FROM post ORDER BY id DESC";
$sth = $dsn->prepare($sql);
$result = $sth->execute();
?>
<?php if ($result !== false && pg_num_rows($result)): ?>
<ul>
<?php while ($post = pg_fetch_assoc($result)): ?>
<li>
<?php echo htmlspecialchars($post['name'], ENT_QUOTES, 'UTF-8'); ?>
<?php echo htmlspecialchars($post['contents'], ENT_QUOTES, 'UTF-8'); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</body>
</html>
php.php
$dsn = new PDO('pgsql:dbname=board host=127.0.0.1 port=5432', 'postgres', 'Bossmanbig123');
So I made a blog with a function to add a post.
This is my code for adding a post
<?php
include_once('resources/init.php');
if(isset($_POST['title'],$_POST['contents'])){
$errors = array();
$title = trim($_POST['title']);
$contents = trim($_POST['contents']);
if(empty($title)){
$errors[] = 'You need to supply a title';
}
else if(strlen($title)>255){
$errors[] = 'The title can not be longer than 255 characters';
}
if(empty($contents)){
$errors[] = 'You need to supply some text';
}
if(empty($errors)){
add_post($title,$contents,$_POST);
header("Location:index.php?id={$id}");
die();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Post</title>
<style>
label{display: block;}
ul{list-style: none;}
li{display: inline; margin-right: 20px;}
</style>
</head>
<body>
<nav>
<ul>
<li><a href='index.php' >Index</a></li>
<li><a href='add_post.php' >Add a Post</a></li>
<!--li><a href='' ></a></li-->
</ul>
</nav>
<h1>Rogier's blog</h1>
<h2>Add a Post</h2>
<?php
if(isset($errors) && !empty($errors)){
echo"<ul><li>",implode("</li><li>",$errors),"</li></ul>";
}
?>
<form action='' method='post'>
<div>
<label for='title'>Title</label>
<input type='text' name='title' value='<?php if(isset($_POST['title'])) echo $_POST['title']; ?>' />
</div>
<div>
<label for='contents'>Content</label>
<textarea name='contents' cols=20 rows=10><?php if(isset($_POST['contents'])) echo $_POST['contents']; ?></textarea>
</div>
<p><input type='submit' value='Add Post' /></p>
</form>
</body>
</html>
and this is my index.php
<?php
include_once('verwerk.php');
include_once('resources/init.php');
//$posts = (isset($_GET['id'])) ? get_posts($_GET['id']) : get_posts();
$posts = get_posts((isset($_GET['id']))? $_GET['id'] : null);
?>
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<style>
ul{list-style: none;}
li{display: inline; margin-right: 20px;}
</style>
</head>
<body>
<nav>
<ul>
<li><a href='index.php' >Index</a></li>
<li><a href='add_post.php' >Add a Post</a></li>
<!--li><a href='' ></a></li-->
</ul>
</nav>
<h1>Rogier's blog</h1>
<?php
foreach($posts as $post){
?>
<h2><a href='index.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a></h2>
<p>
Posted on <?php echo date('d-m-y h:i:s',strtotime($post['date_posted'])); ?>
In AMP<?php echo $post['name']; ?></a>
</p>
<div><?php echo nl2br($post['contents']); ?></div>
<menu>
<ul>
<li><a href='delete_post.php?id=<?php echo $post['post_id']; ?>' >Delete This Post</a></li>
<li><a href='edit_post.php?id=<?php echo $post['post_id']; ?>' >Edit This Post</a></li>
</ul>
</menu>
<?php
}
?>
</body>
</html>
So the problem is when I click on add post it takes me to the add_post.php where I can write stuff down and add it, only problem is when I click on add post it takes me back to the index.php but my post is not shown.
Can someone help me fix this?
Thanks.
I'm pulling content from MySQL database and I would like to display such content in 3 columns. I have managed to achieve it, but not all the content gets pulled. Even when I change it to 4 columns, it displays more content but still not all of them.
There must be a way to check how much content it is and display it correctly. Any help would be much appreciated.
This is what I have so far:
(I'm supposed to display 52 items from database, but only 38 or so get displayed)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="wrapper">
<div id="page">
<div id="content">
<div id="report_content">
<div id="top_bar">
<h1 class="report_title"></h1>
<h2 class="report_subtitle"></h2>
<div id="criteria"></div>
</div>
<div>
<div>
<div id="t1" class="results resultList">
<?php if ($row_detail_vendors) { $temp_name='' ; ?>
<table>
<?php $cols=4 ; do { ?>
<tr>
<?php for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if // the records are less than $cols $row_detail_vendors=m ysql_fetch_assoc($detail_vendors) ?>
<td> <a href="partner_info.php?idu=<?php echo $row_detail_vendors['shortcut']; ?>">
<?php echo ucfirst(strtoupper($row_detail_vendors['hotel_name'])); ?></a>
<br> <span><?php echo $row_detail_vendors['city']; ?>, <?php echo ucfirst($row_detail_vendors['country']); ?></span>
<br/>
<br/>
</a>
</td>
<?php } ?>
</tr>
</div>
<!--marketing-->
<?php } while ($row_detail_vendors=m ysql_fetch_assoc($detail_vendors)); ?>
<?php }?>
</div>
</div>
</div>
<!--marketing partners-->
</div>
<!--content-->
</div>
<!--page-->
</body>
Would advise:
<div id="t1" class="results resultList">
<table>
<?php
$cols=4;
$i = 1;
while($row_detail_vendors=mysql_fetch_assoc($detail_vendors)) {
if($i % $cols == 1){
echo "<tr>"; // start column wrapper
} ?>
<td><?php echo strtoupper($row_detail_vendors['hotel_name'])); ?>
<br /><span><?php echo $row_detail_vendors['city']; ?>, <?php echo ucfirst($row_detail_vendors['country']); ?></span>
<br/>
<br/>
</td>
<?php
if( $i % $cols == 0){
echo "</tr>"; // End column wrapper
}
$i++;
} ?>
</table>
</div>
<!--marketing-->
I have a problem with my code in php.
Error: Undefined Variable 'id'.
<?php
include_once '../includes/connection.php';
include_once '../includes/functions.php';
session_start();
$posts = $pdo->query("SELECT * FROM posty ORDER BY post_id DESC");
$j=0;
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../sources/css/style.css">
<script type="text/javascript" src="../sources/scripts/time.js"></script>
<script type="text/javascript" src="../sources/scripts/scroll.js"></script>
</head>
<body onload="timeLine(); setInterval('timeLine()', 1000 )" >
<header>
<div class = "menu">
<ul>
<li> Blog </li>
<li> Archives </li>
<li> Contact </li>
<li id="addPost"> Add Post </li>
<li id="editPost"> Edit Post </li>
<li id="deletePost"> Delete Post </li>
<li id="showBlogContent"> Hide Blog </li>
<li id="menuRightLogin"> Logout </li>
<li id="menuRightDate"><?php echo date('jS F Y').' '; ?><span id="clock"> </span> </li>
</ul>
</div>
</header>
<div id="showBlog" style="display: block;">
<div class="container">
<div class="postsContainer">
<?php foreach($posts as $post) {
if ($j <= 5) { ?>
<div class="postBox">
<div class="postTitle">
<br />
<?php echo $post['post_title']; ?>
</div>
<div class="postContent">
<br />
<?php if(($wordCount = str_word_count($post['post_content']) <=50)) {
echo $post['post_content'];?>
Edit Post
<br />
<?php } else {
echo excerpts($post['post_content'], 50).'... <br/> <br/>
Edit Post
<br /> <br />';
} ?>
</div>
<div class="postDate">
<?php echo '<b id="author">Author:</b> '.$post['post_author'].' <b id="posted">posted:</b> '.date('jS F Y', $post['add_date']); ?>
</div>
</div>
<?php $j++; } }?>
</div>
</div>
<footer>
<small> © Copyright 2015, n3stis </small>
</footer>
</div>
</body>
</html>
So from here I'm sending a 'id' to edit form:
<?php
include_once '../includes/connection.php';
include_once '../includes/functions.php';
session_start();
$id = $_GET['id'];
if (isset ($id)) {
if (isset($_SESSION['logged_in'])) {
$query = $pdo->prepare("SELECT * FROM posty WHERE post_id='" . $id . "' LIMIT 1");
$query->execute();
$post = $query->fetch();
if (isset($_POST['post_title'], $_POST['post_content'], $_POST['post_author'])) {
$postTitle = $_POST['post_title'];
$postAuthor = $_POST['post_author'];
$postContent = nl2br($_POST['post_content']);
$postTime = time();
if (empty($post_title) or empty($post_content) or empty($post_author)) {
$error = 'All fields required.';
} else {
$sql = ("UPDATE `posty` SET `post_title` = :title, `post_author` = :author, `post_content` = :content, `edit_date` = :editDate WHERE `post_id` = :postID ");
$query = $pdo->prepare($sql);
$query->bindValue(':title', $postTitle);
$query->bindValue(':author', $postAuthor);
$query->bindValue(':content', $postContent);
$query->bindValue(':editDate', $postTime);
$query->bindValue(':postID', $id);
$query->execute();
header('Location: adminPanel.php');
}
}
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../sources/css/style.css">
<script type="text/javascript" src="../sources/scripts/time.js"></script>
<script type="text/javascript" src="../sources/scripts/scroll.js"></script>
</head>
<body onload="timeLine(); setInterval('timeLine()', 1000 )">
<header>
<div class="menu">
<ul>
<li> Blog </li>
<li> Archives </li>
<li> Contact </li>
<li id="#addPost"> Add Post </li>
<li id="#editPost"> Edit Post </li>
<li id="#deletePost"> Delete Post </li>
<li id="showBlogContent"> Hide Blog </li>
<li id="menuRightLogin"> Logout </li>
<li id="menuRightDate"><?php echo date('jS F Y') . ' '; ?><span id="clock"> </span></li>
</ul>
</div>
</header>
<div id="showBlog" style="display: block;">
<div class="container">
<div class="postsContainer">
<?php if (isset($error)) { ?>
<p id="error"><?php echo $error ?> </p>
<?php } ?>
<form action="editPostPanel.php" method="post">
<input type="text" name="post_title" value="<?php echo $post['post_title'];?>"/>
<input type="text" name="post_author" value="<?php echo $post['post_author'];?>"/>
<br/>
<br/>
<textarea name="post_content" rows="15" cols="90"><?php echo $post['post_content'];?></textarea>
<br/>
<br/>
<input type="submit" value="Wyślij post"/>
</form>
</div>
</div>
<footer>
<small> © Copyright 2015, n3stis </small>
</footer>
</div>
</body>
</html>
<?php
} else {
echo 'Error';
} }
else {
echo 'Error';
}
When I send a form I get this:
Notice: Undefined index: id on line 7
I will be very greatfull for yours help :)
change
$id = $_GET['id'];
if (isset ($id))
to
if (isset ($_GET['id'])) {
$id = $_GET['id'];
Your form doesn't have the id parameter in it. You need to change
<form action="editPostPanel.php" method="post">
to:
<form action="editPostPanel.php?id=<?php echo $id; ?>" method="post">
This is in addition to correcting the isset check that the other answers noted:
if (isset($_GET['id'])) {
$id = $_GET['id'];
While I don't have your line numbers, I'd guess that line 7 is $id = $_GET['id'];. This indicates that you don't have a GET (query) param called id set. To fix that Notice, you probably want to swap the two lines as follows:
if (isset($_GET['id'])){
$id = $_GET['id']
But all that will do is start echoing 'Error' back to you. You'll also want to figure out why your line editPostPanel.php?id='.$post['post_id'] is not apparently setting the id as desired.
I am stuck and I am desperate. Here is the problem:
I have a div "container" that includes all the other divs.
But somehow the div ends where the first php statement is! Magic I say.
I want the container div to hold the whole page, but it reaches the first php statement (that includes the code from another file into the page) and ends. I can't find the reason.
Code.
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>{subTITLE}<?php echo TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<meta http-equiv="description" content="{subDESCRIPTION}<?php echo DESCRIPTION; ?>">
<meta http-equiv="keywords" content="<?php echo KEYWORDS; ?>">
<link type="text/css" rel="stylesheet" href="<?php echo ABSWEBTPLPATH; ?>main.css">
</head>
<body>
<div class="superbar">
<div class="menuSearch">
<span>
<?php ShowSearchForm(); ?>
</span>
</div>
</div>
<div class="container">
<?php include(TPLPATH.'header.php'); ?>
<br/><br/><br/>
<div class="content">
<div class="contentLeft">
<div class="cLhead">
<h4>
Categories
</h4>
</div>
<ul class="categories">
<?php ShowMenu(); ?>
</ul>
</div>
<span class="contentRight">
<?php ListGoods(); ?>
</span>
</div>
<?php include(TPLPATH.'footer.php'); ?>
</div>
</body>
</html>
PHP of header.php
<div class="header-container">
<div class="wrapper">
</div>
<div id="header_top_right">
<div class="btn1">
<span class="menuPokupki"><div class="description">Мои покупки</div><div class="descriptionSub"><nobr>история покупок</nobr></div></span>
</div>
<div class="btn2">
<span>
<h5 >Мой магазин</h5>
</span>
</div>
<form action="<?php echo ABSWEBPATH; ?>ch_rt.php" method="post">
<span class="cnt">
Валюта:
<select name="rt" class="sorting">
<?php
session_start();
if(isset($_SESSION["rt"]) && !empty($_SESSION["rt"])) {
if($_SESSION["rt"] == "wmr" or $_SESSION["rt"] != "wmz" or $_SESSION["rt"] != "wme" or $_SESSION["rt"] != "wmu") {
echo "<option value=\"wmr\" selected=\"selected\">руб. (RUR)</option>\r\n"; }
else {echo "<option value=\"wmr\">руб. (RUR)</option>\r\n";}
if($_SESSION["rt"] == "wmz") {
echo "<option value=\"wmz\" selected=\"selected\">$ (USD)</option>\r\n"; }
else {echo "<option value=\"wmz\">$ (USD)</option>\r\n";}
if($_SESSION["rt"] == "wme") {
echo "<option value=\"wme\" selected=\"selected\">€ (EUR)</option>\r\n"; }
else {echo "<option value=\"wme\">€ (EUR)</option>\r\n";}
if($_SESSION["rt"] == "wmu") {
echo "<option value=\"wmu\" selected=\"selected\">грн. (UAH)</option>\r\n"; }
else {echo "<option value=\"wmu\">грн. (UAH)</option>\r\n";} }
else {
if(isset($default_rt) && !empty($default_rt)) {
if($default_rt == "wmr" or $default_rt != "wmz" or $default_rt != "wme" or $default_rt != "wmu") {
echo "<option value=\"wmr\" selected=\"selected\">руб. (RUR)</option>\r\n"; }
else {echo "<option value=\"wmr\">руб. (RUR)</option>\r\n";}
if($default_rt == "wmz") {
echo "<option value=\"wmz\" selected=\"selected\">$ (USD)</option>\r\n"; }
else {echo "<option value=\"wmz\">$ (USD)</option>\r\n";}
if($default_rt == "wme") {
echo "<option value=\"wme\" selected=\"selected\">€ (EUR)</option>\r\n"; }
else {echo "<option value=\"wme\">€ (EUR)</option>\r\n";}
if($default_rt == "wmu") {
echo "<option value=\"wmu\" selected=\"selected\">грн. (UAH)</option>\r\n"; }
else {echo "<option value=\"wmu\">грн. (UAH)</option>\r\n";} }
else {
echo "<option value=\"wmr\" selected=\"selected\">руб. (RUR)</option>
<option value=\"wmz\">$ (USD)</option>
<option value=\"wme\">€ (EUR)</option>
<option value=\"wmu\">грн. (UAH)</option>\r\n"; } }
?>
</select>
<input type="submit" value="Установить" class="button" />
</form></span></div>
</div>
<div class="subheader">
<ul class="menu">
<li class="menu1" noWrap>О магазине</li>
<li class="menu1" noWrap>Способы оплаты</li>
<li class="menu1" noWrap>Контакты</li>
<span class="filter">
<span class="sortingTitle">Сортировка:</span>
<span class="sortingSelect">
<?php GoodsSort(); ?>
</span>
</span>
</ul>
</div>
</div>
</div>
CSS has 500 lines so I uploaded it to pastebin
sorry for terrible crap code. I have to urgently edit someone else's and I already hate him
http://pastebin.com/6b5UC9mj
Too many closing divs in header.php
Take the last one out
Your span is closed outside the form. Your current structure is :
<form action="<?php echo ABSWEBPATH; ?>ch_rt.php" method="post">
<span class="cnt">
....
</form>
</span>
Must be:
<form action="<?php echo ABSWEBPATH; ?>ch_rt.php" method="post">
<span class="cnt">
....
</span>
</form>