In my view i have a foreach statement which grabs all the nessesary data from the database which displays correctly.
Here is the view:
<?php
?>
<div class="cmt-container" >
<?php
foreach($results as $row){
$user = $row->user;
$comment = $row->comment;
$date = $row->date;
$name = $row->name;
$joke = $row->joke;
$joke_id = $row->joke_id;
// Get gravatar Image
// https://fr.gravatar.com/site/implement/images/php/
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/"."?d=".$default."&s=".$size;
?>
<div class="cmt-cnt">
<img src="<?php echo $grav_url; ?>" />
<div class="thecom">
<h5><?php echo $user; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
</div>
</div><!-- end "cmt-cnt" -->
<?php
}
?>
<?php
echo form_open('comments/insertComment');
?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="joke_id">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php
echo form_close();
?>
My question is, how can i pass the $joke_id variable to the insertComment function in my comments controller.
I have put the input field as hidden on the joke_id field because i want to assign the ID of a joke to a comment, so the joke will have unique comments.
If the comments are on the same page as the joke, you can just take the $joke_id and put it in the hidden input:
<input type="hidden" name="joke_id" value="<?php echo $joke_id; ?>">
And when you add a comment (I assume you handle the form datas in the insertComment() function), you can access the joke id by $_POST['joke_id'].
(yes, that's not really secure but if your user can comment any joke, you just have to check that a joke with the id equal to $_POST['joke_id'] exists in the DB and if so, you just insert the comment)
Is that what you wanted?
try this..
<?php echo form_open('comments/insertComment'); ?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="jokeid" value="<?= $joke_id; ?>">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php echo form_close();?>
and in your insert comment controller
public function __construct() {
//codes here
$joke_id = $this->input->post('jokeid');
}
Related
I would like to create a form where I can upload an image and add a Name dropdown field with values coming from a table namely first name and last name using CONCAT to combine them into a complete name.
The dropdown field has the values coming from the table and works just fine.
My only problem is I can't get the value of the dropdown field to store into a variable so I can include it on the insert query.
I tried getting the value of the option tag through the post method by getting it from the select tag using the name property.
<?php
include('adminheader.php');
include('connection.php');
if (isset($_POST["insert"])) {
if (!$_POST['description']) {
$error = "<p>Description field is required</p>";
}
$ownerselected = mysqli_real_escape_string($link, $_POST['owner']);
$ownerselected1 = "hello";
$imagedescription = mysqli_real_escape_string($link, $_POST['description']);
$file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$query = "INSERT INTO images(name,description,ownerid,uploaderid) VALUES ('$file','$imagedescription','$ownerselected','$userLoggedIn')";
if (mysqli_query($link, $query)) {
$notification = "Image successfully inserted " . $ownerselected1;
}
}
?>
<br/><br/>
<div class="imagecontainer container">
<div class="logincontainer">
<div id="error"><?php echo $error; ?></div>
<div id="error"><?php echo $notification; ?></div>
<div style="width:500px;">
<h3 align="center"><strong>Select an Image to upload</strong></h3>
<br/>
<form method="post" enctype="multipart/form-data">
<?php
$query = "SELECT CONCAT(fname,' ',lname)AS Name FROM person";
$result = mysqli_query($link, $query);
?>
<br/>
<div class="form-group"> <!--Start of image upload -->
<label for="image">Click Browse to locate image</label>
<input type="file" name="image" id="image"/> <!--end of image upload -->
<br/>
<div class="form-group"> <!--Start of image description -->
<label for="description">Image Description</label>
<input type="text" class="form-control" name="description" id="description"
placeholder="Enter txt">
</div> <!--end of image description -->
<div class="form-group"><!--start of owner dropdown -->
<label for="owner">Image Owner</label>
<select class="form-control" id="owner" name="owner">
<?php while ($row1 = mysqli_fetch_array($result)):; ?>
<option value="<?php echo $row1['Name']; ?>">
<?php echo $row1['Name']; ?>
</option>
<?php endwhile; ?>
</select>
</div> <!--end of owner dropdown -->
<input type="submit" name="insert" id="insert" value="Insert" class="btn btn-info"/>
</form>
<br/>
<br/>
When I run the script, everything looks fine on the front end but when I perform the insert, only the image gets written on the table and imageowner column has no value. Any help is greatly appreciated. Thanks!
So I am trying to learn php, I have a simple 1 item order form, However I have 4 items in the data base I need to show on this order form. In javascript I would just write a loop for an array, however the whole php query form a table from a database has got me flustered and I am not sure how to loop the results to show more then the 1 item from the query and the still allow for ordering all on 1 page. Originaly I had it list a table so you would click on the item and it would redirect to a page for each item so each on can be ordered.
<?php
include_once 'helpers/helper.php';
include_once 'db_function.php';
session_start();
$show_query = "SELECT * FROM `meal_info` ORDER BY meal_time DESC LIMIT 4;";
$show_result = db_query($connect, $show_query);
if($show_result) {
$show_result_row = db_fetch_array($show_result);
}
?>
<?php include_once 'layout/header.php'; ?>
<?php
if(#$_SESSION['order_submit_msg']) {
echo flash_message('order_submit_msg');
unset($_SESSION['order_submit_msg']);
}
?>
<div>
<form action="make_deal.php" method="post">
<div>
<h1><?php echo $show_result_row['meal_name'];?></h1>
</div>
<hr>
<div>
<img class="meal-img" id="meal-img" src="<?php echo $show_result_row['meal_image'];?>" alt="">
</div>
<br>
<div>
<h2>Feature: <?php echo $show_result_row['meal_content'];?></h2>
<h3>Price: <?php echo $show_result_row['meal_price'];?></h3>
</div>
<input type="hidden" name="meal-id" value="<?php echo $show_result_row['meal_id'];?>">
<input type="hidden" name="meal-price" value="<?php echo $show_result_row['meal_price'];?>">
<br>
<div>
<legend>Name</legend>
<input type="text" name="order-username" placeholder="Full Name" required>
</div>
<br>
<div>
<legend>Phone</legend>
<input type="text" name="order-phone" placeholder="555-555-5555" required>
</div>
<br>
<div>
<legend>Address</legend>
<input type="text" name="order-address" placeholder="555 N. Main Street" required>
</div>
<br>
<div>
<legend>Quantity (Dozens)</legend>
<input type="text" name="order-count" placeholder="12" required>
</div>
<hr>
<div>
<button class="btn btn-warning btn-lg btn-block" type="submit">Submit Order</button>
</div>
</form>
</div>
<?php include_once 'layout/footer.php'; ?>
You're trying to display an array. You need to loop through the array.
<?php foreach ($show_result_row as $result) { ?>
<div>
<h1><?php echo $result['meal_name'];?></h1>
</div>
<hr>
<div>
<img class="meal-img" id="meal-img" src="<?php echo $result['meal_image'];?>" alt="">
</div>
<?php } ?>
Something like that.
I am trying to create an array of objects which I can then display, the objects being created when a form has been submitted.
The first object gets successfully added, but when I add another object, it simply overwrites the last created object. Can anyone see where I am going wrong?
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/classes/Goal.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/global/head.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/config/init.php'; ?>
<?php
$input['title'] = "";
$input['deadline'] = "";
$input['description'] = "";
if(!isset($_SESSION['goals'])) {$_SESSION['goals'] = array();}
if (isset($_POST['submit'])) {
$_SESSION['goalCount'] ++;
$input['title'] = htmlentities($_POST ['title'],ENT_QUOTES);
$input['deadline'] = htmlentities($_POST ['deadline'],ENT_QUOTES);
$input['description'] = htmlentities($_POST ['description'],ENT_QUOTES);
convertDate($input['deadline']);
${'goal'. $_SESSION['goalCount']} = new Goal($input['title'], $input['description'], $_SESSION['username'], $input['deadline']);
array_push($_SESSION['goals'], ${'goal'. $_SESSION['goalCount']});
?>
<div class="top">
<p>h</p>
</div>
<div class="container">
<div class="sixteen columns topbar">
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/header.php'; ?><!-- Content Begins -->
<div class="content">
<h1> OO Test Page - Batch add goals</h1><hr/>
<div class="six columns">
<form action="" method="post">
<fieldset>
<div>
<h4>Title</h4>
<span id='title-result'></span>
<label for="title"></label><br />
<input type="text" id="title" name="title" placeholder="e.g. Graduate" value="" required aria-required="true">
</div>
<div>
<h4>Description</h4>
<span id='description-result'></span>
<label for="description"></label>
<textarea type="description" id="description" placeholder="e.g. with first-class honours" name="description" value="" required aria-required="true"></textarea>
</div>
<div>
<h4>Deadline</h4>
<span id='description-result'></span>
<label for="deadline"></label>
<input rows="2"type="date" id="datepick" placeholder="" name="deadline" value="" required aria-required="true"/>
</div>
<div class="submit">
<input type="submit" name="submit" value="Add">
</div>
</fieldset>
</form></div>
<div class="ten columns">
<?php
foreach ($_SESSION['goals'] as $goal)
{
echo '<div class="goal"><h4>'. $goal->title .'</h4>'. $goal->desc .'</h4><p>'. $goalCount .'</p></div>';
}
echo Goal::$counter;
var_dump($_SESSION['goals'])
?>
</div>
</div>
<!-- Content Ends -->
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/footer.php'; ?>
</div>
</div>
</body>
</html>
Simply initialize that session variables that you need. And no need to use variable variables and using it as a counter to push inside. Just normally push those object inside the session.
Example:
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/classes/Goal.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/global/head.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/config/init.php'; ?>
<?php
if(!isset($_SESSION['goals'], $_SESSION['goalCount'])) {
$_SESSION['goals'] = array();
$_SESSION['goalCount'] = 0;
}
if (isset($_POST['submit'])) {
$_SESSION['goalCount'] += 1;
$input['title'] = htmlentities($_POST ['title'],ENT_QUOTES);
$input['deadline'] = htmlentities($_POST ['deadline'],ENT_QUOTES);
$input['description'] = htmlentities($_POST['description'],ENT_QUOTES);
convertDate($input['deadline']);
$goal = new Goal($input['title'], $input['description'], $_SESSION['username'], $input['deadline']);
$_SESSION['goals'][] = $goal;
// ^ add another dimension
} // missing closing curly brace
?>
<div class="top">
<p>h</p>
</div>
<div class="container">
<div class="sixteen columns topbar">
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/header.php'; ?><!-- Content Begins -->
<div class="content">
<h1> OO Test Page - Batch add goals</h1><hr/>
<div class="six columns">
<form action="" method="POST">
<fieldset>
<div>
<h4>Title</h4>
<span id='title-result'></span>
<label for="title"></label><br />
<input type="text" id="title" name="title" placeholder="e.g. Graduate" value="" required aria-required="true">
</div>
<div>
<h4>Description</h4>
<span id='description-result'></span>
<label for="description"></label>
<textarea type="description" id="description" placeholder="e.g. with first-class honours" name="description" value="" required aria-required="true"></textarea>
</div>
<div>
<h4>Deadline</h4>
<span id='description-result'></span>
<label for="deadline"></label>
<input rows="2"type="date" id="datepick" placeholder="" name="deadline" value="" required aria-required="true"/>
</div>
<div class="submit">
<input type="submit" name="submit" value="Add">
</div>
</fieldset>
</form>
</div>
</div>
<div class="ten columns">
<?php
foreach ($_SESSION['goals'] as $goal) {
echo '<div class="goal"><h4>'. $goal->title .'</h4>'. $goal->desc .'</h4><p>'. $goalCount .'</p></div>';
}
?>
</div>
<!-- Content Ends -->
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/footer.php'; ?>
</div>
</div>
Sidenote: Always turn on error reporting.
I trying to make a feature, where the user recieves a message saying "Your changes have been saved" after the user has pressed "save changes". When the form is being submittet the page is just refreshed and not redirected to any other page.
I have tried several things but nothing seems to work. I am good at HTML but not PHP. I would love if any of you could help me out!
My code for my profile template is the folowing:
<div class="user-image">
<div class="bordered-image thick-border">
<?php echo get_avatar(ThemexUser::$data['user']['ID'], 200); ?>
</div>
<div class="user-image-uploader">
<form action="<?php echo themex_url(); ?>" enctype="multipart/form-data" method="POST">
<label for="avatar" class="button"><span class="button-icon upload"></span><?php _e('Skift billede','academy'); ?></label>
<input type="file" class="shifted" id="avatar" name="avatar" />
<input type="hidden" name="user_action" value="update_avatar" />
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(THEMEX_PREFIX.'nonce'); ?>" />
</form>
</div>
</div>
<div class="user-description">
<form action="<?php echo themex_url(); ?>" class="formatted-form" method="POST">
<div class="message">
<?php ThemexInterface::renderMessages(); ?>
</div>
<div class="sixcol column">
<div class="field-wrapper">
<input type="text" name="first_name" size="30" value="<?php echo ThemexUser::$data['user']['profile']['first_name']; ?>" placeholder="<?php _e('Fornavn','academy'); ?>" />
</div>
</div>
<div class="sixcol column last">
<div class="field-wrapper">
<input type="text" name="last_name" size="30" value="<?php echo ThemexUser::$data['user']['profile']['last_name']; ?>" placeholder="<?php _e('Efternavn','academy'); ?>" />
</div>
</div>
<div class="clear"></div>
<!-- ADRESSE -->
<?php if(!ThemexCore::checkOption('profile_signature')) { ?>
<div class="field-wrapper">
<input type="text" name="signature" value="<?php echo ThemexUser::$data['user']['profile']['signature']; ?>" placeholder="<?php _e('Adresse','academy'); ?>" />
</div>
<?php } ?>
<div class="user-fields">
<?php ThemexForm::renderData('profile', array(), ThemexUser::$data['user']['profile']); ?>
</div>
<?php } ?>
<span class="button-icon save"> </span><?php _e('Gem ændringer','academy'); ?>
<input type="hidden" name="user_action" value="update_profile" />
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(THEMEX_PREFIX.'nonce'); ?>" />
</form>
</div>
you are submitting to another url. So you can add something like the below to your functions file to display a message when the url is appended with msg. (you can account for multiple messages using the switch below, look it up) also you will need to add `?msg=yourmessagevariable' to your form action.
add_action('wp_print_scripts', 'notifcation', 100);
function notifcation () {
if($_GET['msg']) {
$message= sanitize_text_field($_GET['msg']);
switch($message) {
case 'reg-business':
$notehead= 'Please register first';
$msg= 'Please register your business before continuing';
break;
case 'nowvalid':
$notehead= "Congratulations";
$msg='You have successfully connected ';
break;
case 'Completed':
$notehead= 'Edited';
$msg='You have successfully edited your advert';
break;
}
?>
<section id="notification" class="notif notif-notice">
<h6 class="notif-title"><?php echo $notehead;?></h6>
<p><?php echo $msg; ?></p>
<div class="notif-controls">
Close
</div>
</section>
<script type="text/javascript">
jQuery(document).ready(function() {
setTimeout(function(){
jQuery('#notification').css('display', 'none');
},3000)
});
jQuery('.notif-close').click(function(e){
e.preventDefault();
jQuery(this).parent().parent().css('display', 'none');
})
</script>
<?php
}
}
Hello i have a problem i cant see the comments on the page.
I dont get any error on the page so i am stuck on this moment
can somebody help me??
This is the php code:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
and the rest of the page is:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
Hope somebody see the problem.
try like this for printing comments..
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()){ //opening while block
?>
<div>
<h5><?php echo $row->name;?></h5>
<blockquote><?php echo $row->comment;?></blockquote>
<?php }// ending while block ?>
</div>
</div>
<div id="comments">
<?PHP
$query = $db->query("SELECT name, comment FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()) {
?>
<div>
<h5><?= $row->name ?></h5>
<blockquote><?= $row->comment ?></blockquote>
</div>
<?PHP } ?>
</div>
If that does not work, i would suggest dumping out the SQL query and making sure it is working.