receiving either value for the navigation menu in php - php

I have two navigation one is in the header and the other is in the same page, now what I want is to do is be able to pull all the data from the table product a specific product from the sub category when I click the sub category. what I have now, is when I click on the menu that is on the header page that I have included in every page I get the product listed but the menu that is on product page produce an error that the foreach is empty. when I use my other function to pull all the sub category it works fine but I want to pull only the sub category that belong to that particular main category. the code below will make more sense.
header Page:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');?>
<?php require_once("initi.php");?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/main.css" />
<link rel="stylesheet" href="css/footer.css" />
<script type="text/javascript" src="/ghaniya.com/javascript/jqu.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".logbtn").click(function(){
$("#login2").hide("show");
$("#login1").slideToggle("slow");
$(this).toggleClass("active");
});
$(".logbtn2").click(function(){
$("#login1").hide("show");
$("#login2").slideToggle("slow");
$(this).toggleClass("active");
});
});
</script>
<style type="text/css">
div#login1{
background-color:#efebee;
margin:0 auto;
width:100%;
display:none;
height:100px;
}
div#login2{
background-color:#f5dce4;
margin:0 auto;
width:100%;
display:none;
height:100px;
}
.size{width:960px; }
.flotl{float:left; font-size:11px; text-decoration:none; height:30px; margin-left:20px;}
div#centerl{ width:960px; margin:0 auto; }
</style>
</head>
<body>
<div id="head">
<div class="size center">
<img src="img/logosmall.png">
<ul id="mainLink">
<li><a class="logbtn">Women</a></li><li><a class="logbtn2" href="#">man</a></li><li>Beauty</li><li>Gifts</li><li>Perfumes</li>
</ul>
<div id="loginBox">
<h3>REGISTER | SIGN IN</h3>
</div>
<div id="search">
<input type="submit"/><input type="text" value="Search"/>
</div>
<div id="bag"></div>
</div>
</div>
<div id="login1" >
<div id="centerl">
<ul class="flotl" >
<?php
$cata = Catagory::find_all();
foreach($cata as $catag){?>
<li><?php echo $catag->name; ?></li>
<?php }?>
</ul>
</div>
</div>
Product Page:
<?php require_once("includes/head.php"); ?>
<?php
$cata_id = "";
$subcat ="";
if(isset($_GET['catid']))
{
$cata_id = $_GET['catid'];
$product = Product::find_by_cata_id($cata_id);
}
elseif(isset($_GET['subcat']))
{
$subcat =$_GET['subcat'];
$subcat = SubCata::find_by_cata_id(1);
print_r($subcat);
}
?>
<div class="cBoth"></div>
<div id="contentEveryWhere" class="center">
<div id="cata">
<div id="leftNoticeBoard"></div>
<ul id="catagories">
<h1>CLOTHING</h1>
<?php foreach($subcat as $sub){ ?>
<li><?php echo $sub->name; ?></li>
<?php } ?>
</ul>
</div>
<div id="products">
<div id="catatitle">
<ul>
<li>Home ></li><li>Women ></li><li>Coat<li>
</ul>
<h1>Coat</h1>
<p class="floatl">Sort items by Price: High | Low View as: <img src="#" alt="icon png"/> |<img src="#" alt="icon png"/></p>
</div>
<ul id="product">
<?php $product = Product::find_by_cata_id($cata_id);?>
<?php foreach($product as $pro){?>
<?php $image = Image::find_by_product_id( $pro->product_id);?>
<li>
<?php if(isset($image->filename)){ ?>
<img src="/ghaniya.com/img/products/<?php echo $image->filename;?>"/>
<?php } ?>
<h2><?php echo $pro->name;?></h2>
<h3> <?php echo $pro->product_desc;?></h3>
<h4><?php echo $pro->price;?></h4>
</li>
<?php } ?>
</ul><!--end of product list-->
</div> <!--end of products div-->
</div><!--end of contentEveryWhere div-->
<div class="cBoth"></div>
<?php require_once("includes/footer.php"); ?>
Warning: Invalid argument supplied for foreach() in /Users/mughery/Website/ghaniya.com/product.php on line 25

The problem is quite simple:
if(isset($_GET['catid']))
{
$cata_id = $_GET['catid'];
$product = Product::find_by_cata_id($cata_id);
}
elseif(isset($_GET['subcat']))
{
$subcat =$_GET['subcat'];
$subcat = SubCata::find_by_cata_id(1);
}
That means you're only setting $subcat if, and only if $_GET['catid'] isn't set and $_GET['subcat'] is. In all other cases $subcat is an empty string, not an array. Just add a line like:
$subcat = (is_array($subcat) ? $subcat : SubCata::find_by_cata_id(1));
//or:
$subcat = (is_array($subcat) ? $subcat : array());
And you'll be good to go
Also, this:
$subcat =$_GET['subcat'];
$subcat = SubCata::find_by_cata_id(1);
Doesn't really make sense to me, I think you need something like:
elseif(isset($_GET['subcat']))
{
$subcat =SubCata::find_by_cata_id($_GET['subcat']);
}
$subcat = (is_array($subcat) ? $subcat : SubCata::find_by_cata_id(1));
If not, regardless of the subcat parameters actual value, your fetching all data for id 1...

When using a foreach, the first argument has to be a array.
You could change the code to:
<?php
if( is_array( $subcat ) )
foreach($subcat as $sub){
?>

4 line:
<?php
$subcat = array();
?>

Related

Blog, adding post doesn't work

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.

Get a Primary Key from <a href> for it's content to be visible on another page with Code Igniter

I am completely new to Code Igniter, and heres my issue:
I am making a Cinema Website, and im trying to get the all of the movies resume
in one page, for example, when i pick a movie on the index, it should open me
a new link that gives me the movie details that i got on my database.
This is the php from the index
Filmes
<div id="films-tab">
<?php
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
?>
<li><?php echo $row->Nome; ?></li>
<?php
}
?>
</div>
</ul>
</li>
This is the php for the "briefing page"
<link href="assets/recursos/outro.css" rel="stylesheet">
<div class="container-fluid">
<?php
$query = $this->db->get_where('filme', ['id' => $id]);
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
$row = $query->first_row();
?>
<img style="margin-right:20px; border:5px solid white; width:320px; height:468px;"src="<?php echo 'assets/recursos/images/'.$row->Capa; ?>">
<?php
}
?>
</div>
I know the code to show only 1 key in the briefing.php is wrong, but i have no clue how to get it to work, can someone help me?
pass id in url as parameter and get data from database of that id.
pass id in index page's url
<div id="films-tab">
<?php
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
?>
<li><?php echo $row->Nome; ?></li>
<?php
}
?>
</div>
</ul>
</li>
and fetch id from url in briefing page
<link href="assets/recursos/outro.css" rel="stylesheet">
<div class="container-fluid">
<?php
$id = $this->uri->segment(2);
$query = $this->db->get_where('filme', ['id' => $id]);
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
$row = $query->first_row();
?>
<img style="margin-right:20px; border:5px solid white; width:320px; height:468px;"src="<?php echo 'assets/recursos/images/'.$row->Capa; ?>">
<?php
}
?>
</div>

i want to display ads in a foreach loop in codeigniter

i have some items i want displayed in the view, my per page is set to 15, so each page displays 15 items, but i want to display ads in the middle of the loop, after maybe 4 items displayed, I've tried to put an if statement in the foreach loop to control the display, it displays the first 3 values, but when i put the div for the ad, it loops too, can someone please tell me what to do, or point me in the right direction?? thanks, here is my code so far:
<?php
$counter1 = 0;
//the foreach loop that retrieves the values from the controller
foreach($records as $record){
//an if statement to display the first 4 items..
if ($counter1 <= 3){
?>
<div class='box-scene'>
<div class='dbox'>
<div class='front face'>
<img src="images/newtag.png">
</div>
<a style="font-size:15px;" href="<?php echo base_url();?>music/<?php echo $record->url; ?>">
<div class="side face">
<span>
<?php echo $record->name; ?>
</span>
</div>
</a>
</div>
</div>
<?php
$counter1++;
}
?>
<div style="width:200px; height:200px; float:left; display:inline-block; margin: 0 12.5px 20px 12.5px;">
<div id="ad_200_200">
</div>
</div>
<!-- this div displays more than once, i dont know where to place it
for it to display after the first 3 items -->
<?php
}
?>
i want to know where to place the div, and how to continue displaying the items...thanks
Hi please use else condition.please check replace with below code
<?php
$counter1 = 0;
//the foreach loop that retrieves the values from the controller
foreach ($records as $record) {
//an if statement to display the first 4 items..
?>
<?php if($counter1 % 4 == 0) { ?>
<div style="width:200px; height:200px; float:left; display:inline-block; margin: 0 12.5px 20px 12.5px;">
<div id="ad_200_200">
</div>
</div>
<?php } ?>
<div class='box-scene'>
<div class='dbox'>
<div class='front face'>
<img src="images/newtag.png">
</div>
<a style="font-size:15px;" href="<?php echo base_url(); ?>music/<?php echo $record->url; ?>">
<div class="side face">
<span>
<?php echo $record->name; ?>
</span>
</div>
</a>
</div>
</div>
<!-- this div displays more than once, i dont know where to place it
for it to display after the first 3 items -->
<?php
$counter1++;
}
?>
use also an else statement like below:
<?php
$counter1 = 0;
foreach($records as $record){
if ($counter1 <= 3){ ?>
<div class='box-scene'>
<div class='dbox'>
<div class='front face'>
<img src="images/newtag.png">
</div>
<a style="font-size:15px;" href="<?php echo base_url();?>music/<?php echo $record->url; ?>">
<div class="side face">
<span>
<?php echo $record->name; ?>
</span>
</div>
</a>
</div>
<div>
<?php
$counter1++;
} else{ ?>
<div style="width:200px;height:200px;float:left;display:inline-block; margin: 0 12.5px 20px 12.5px;">
<div id="ad_200_200"></div>
</div>
<?php
$counter1 = 0;
} ?>
<?php
}
?>

Update Query in PHP - undefined ID error in code

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.

adding a more link based on the number of entries

I have this code that prints out up to 4 service providers. once clicked, the box expands and shows the rest if there are more. What i am trying to do is if there are more than 4 entries, to print a More link so users know to click for more. I also need the More button to disappear when click and reappear when reclicked.
Can anyone help me. This is driving me crazy
Thank you for your help ahead of time.
<?php
/**
* #file
*/
?>
<head>
<script>
var remove=function(){
$('#ID_OF_BUTTON').hide(500);
}
</script>
</head>
<div class="cloud-computing-item">
<div class="container">
<div class="item-header">
<h3> <?php print $company['name'] ?> </h3>
</div>
<div class="item-subheader">
<div class="label">Services Offered:</div>
<div class="data service-offerings">
<?php
foreach($company['services_display'] as $service => $element){
print $element;
}
?>
</div>
</div>
<div class="item-body">
<div class="overview">
<div class="label">Cloud Providers:</div>
<div class="data">
<?php
//limit shown entries upto 4
foreach(array_slice($company['service_providers'], 0, 4) as $provider): ?>
<div>
<?php print $provider; ?>
</div>
<?php endforeach; ?>
<div id="show"style="color:#000099;font-weight:bold; margin-bottom:-13px;"></div>
<!--<div id="show"style="color:#000099;font-weight:bold; margin-bottom:-13px;"><a onclick="remove(); return true;">More</a></div>
<div id="hide"style="color:#000099;font-weight:bold; margin-bottom:-12px; display: none;"><a onclick="add(); return true;">Hide</a></div> -->
</div>
</div>
<div class="details">
<?php
// if entries are greater then 4, show the rest
foreach(array_slice($company['service_providers'], 4) as $provider): ?>
<div>
<?php
print $provider;
?>
</div>
<?php endforeach; ?>
<?php print theme('cloud_computing_item_details', array('company' => $company)); ?>
</div>
</div>
<div style="clear: both; height: 5px;"> </div>
</div>
</div>
So, try this modification:
<div class="label">Cloud Providers:</div>
<div class="data">
<?php $i = 0; ?>
<?php foreach($company['service_providers'] as $provider): ?>
<div<?php echo ($i > 3) ? ' class="hide"' : ''; ?>><?php print $provider; ?></div>
<?php $i++; ?>
<?php endforeach; ?>
<?php if(count($company['service_providers']) > 4):?>
<div class="show-more" style="color:#000099;font-weight:bold; margin-bottom:-13px;">Show More</div>
<?php endif; ?>
</div>
Now let's assume You are using jQuery add this:
<script type="text/javascript">
$(document).ready(function(
$('.hide').hide(); // this will hide all the elements with the class 'hide'
$('.show-more').live('click', function(){
var parent = $(this).parent();
parent.find('.hide').show().removeClass('hide').addClass('show');
$(this).text('Show Less').removeClass('show-more').addClass('show-less');
});
$('.show-less').live('click', function(){
var parent = $(this).parent();
parent.find('.show').hide().removeClass('show').addClass('hide');
$(this).text('Show Less').removeClass('show-less').addClass('show-more');
});
){});
</script>
Use function on instead of live if You are using jQuery higher then 1.7.
Fiddle: http://jsfiddle.net/eznnC/ (though the hiding is not working but I believe it will in normal environment).
the first part to make only the 4 or more show a More link is below. the link doesn't disappear at the moment nor does it reappear.
<?php
//limit shown entries upto 4
foreach(array_slice($company['service_providers'], 0, 4) as $provider): ?>
<div>
<?php print $provider; ?>
</div>
<?php endforeach; ?>
<?php
// shows a More link for companies with more than for providers
foreach(array_slice($company['service_providers'], 4, 1) as $provider): ?>
<div>
<div id="more" style="color:#000099;font-weight:bold; margin-bottom:-13px;">
<?php print ('<a onclick="">More</a>'); ?>
</div>
</div>
<?php endforeach; ?>

Categories