How do i pass value in url? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how do i create the url of the links? I want when users click on action it will redirect users to the action page.But i don't want to create pages for the links i have provided below.what i want to do is to display different genres type content on the same page;
here is my html
<ul class="snav">
<li>Action</li>
<li>Art and Experimental</li>
<li>Comedy</li>
<li>Crime and Mystery</li>
<li>Documentary</li>
<li>Drama</li>
<li>Epic and Historical</li>
<li>Family</li>
<li>Fantasy</li>
<li>Foreign</li>
<li>Horror</li>
<li>Live Performances</li>
<li>Musicals</li>
<li>Romance</li>
<li>Science Fiction</li>
<li>Special Interest</li>
<li>Sports</li>
<li>Thriller</li>
<li>War</li>
<li>Westerns</li>
</ul>

One way to accomplish this is to use query strings to form a url based off your base url.
http://domain.tld/index.php?genre=crime
index.php
if ($_GET['genre'] == 'crime') {
// display crime genre
} else {
// display index
}
You'll also want to include some validation/error checking when you're doing this. You can check whether or not the genre param is set using isset(). It is also important to escape the input to 'genre' so that injection attacks are less likely.

To generate the links and change page content all in one go, you can try:
<?php
$actions = array("Action", "Art and Experimental", "Comedy", "Crime and Mystery", "Documentary", "Drama", "Epic and Historical", "Family", "Fantasy", "Foreign", "Horror", "Live Performances", "Musicals", "Romance", "Science Fiction", "Special Interest", "Sports", "Thriller", "War", "Westerns");
?>
<ul class="snav">
<?php
foreach($actions as $action) {
$url_token = str_replace(' ', '_', strtolower($action));
echo '<li>' . $action . '</li>';
}
?>
</ul>
<?php
$genre = $_GET['genre'];
switch ($genre) {
case 'action':
echo 'Woo Action!';
break;
case 'drama':
echo 'GASP!';
break;
case 'art_and_experimental':
echo 'Weird...';
break;
default:
echo 'There is no action called ' . $genre . ' :(';
}
?>
This will generate html of the form:
<ul class="snav">
<li>Action</li>
<li>Art and Experimental</li>
...
</ul>

You're going to have to use $_GET variables using PHP and have something like:
Links look like
Action
PHP file:
if (isset($_GET['genre'])) {
switch ($_GET['genre']) {
case 'action':
echo 'something here';
break;
case 'horror':
echo 'something here';
break;
//etc
}
}
or you could use jQuery to dynamically load the data into the page.
Hope this helps!

Related

using <a href> for a mysqli select statement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
could someone please direct me to a simple (basic easy) tutorial or resource page of how to use hyperlinks in a website to display the results of a select statement
To be precise - I am working on the footer section of my website http://www.mandyevansartist.com
when someone clicks on the words 'coloured pencils' I would like them to be taken to the catagory.php page and be shown an array from the database
I know this is possible because I already have made it work by clicking an image - (there is an area in the http://www.mandyevansartist.com/gallery.php page that says 'click on a picture to see others in that catagory')
I have achieved this by
<?php
session_start();
include 'header.php';
echo '<h1>CLICK ON AN IMAGE TO SEE OTHERS IN THAT CATAGORY</h1>';
$con = mysqli_connect("*","*","*","*");
$db = mysqli_select_db("images", $con);
$answer = mysqli_query($con,"SELECT image FROM images where HEAD = 'true'");
echo '<div id = "list">';
echo '<ul>';
while ($row = mysqli_fetch_array($answer)) {
$pic = $row[image];
$link ="<a href = 'catagory.php?id=".$row[image]."'>" . ' <img src="'.$pic.'" style ="height:222px;"/> '. "</a>";
echo '<li>' .$link.'</li>';
}
echo '</ul>';
echo '</div>';
?>
At this point my footer.php is mainly html (enclosed in an echo'') with links going to nowhere
<div id="footer-one">
<h1>GALLERY</h1>
<p><a href = "#" >people pictures</a></p>
<p><a href = "#" >romance</a></p>
<p><a href = "#" >seascapes</a></p>
<p><a href = "#" >under the ocean</a></p>
<p><a href = "#" >paintings</a></p>
<p><a href = "#" >love heart series</a></p>
<p><a href = "#" >new works</a></p>
</div><!--/footer-one-->
one of the ways I have tried to insert a mysqli query into it
<h1>GALLERY</h1>';
$answer=mysqli_query($con,"SELECT image FROM images WHERE catagory = pencils");
while ($answer2 = mysqli_fetch_array($answer));
$link = <p>family portraits</p>
echo '<p>' .$link.'</p>';
echo' <p><a href = "#" >coloured pencils</a></p>
which just comes up with a parse error - unexpected "<"
I am looking for a tutorial to guide me through this process because i have looked and looked and cant find one
The error parse error - unexpected "<" is due to some missing stuff like ', " and {}. Try following fixes.
<?php
$answer = mysqli_query($con,"SELECT image FROM images WHERE catagory = pencils");
while ($answer2 = mysqli_fetch_array($answer));
{
$img = $answer2['image'];
$link = "<a href = 'catagory.php?id=$img'>family portraits</a></p>";
}
echo '<p>'.$link.'</p>';
echo '<p><a href = "#" >coloured pencils</a></p>';
?>
From the given code, I have come up with these fixes. But there may be some changes as per your requirements.

Use echo in a css class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am using php echo throughout different pages on a project, to update a percentage value that is changing daily. This value can be negative or positive.
Here is my code:
<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[n]; ?></strong></i>
I am using FontAwesome icons with a Bootstrap template. Everything is working fine like this.
Now, if the percentage is negative, I would like to use class="icon-thumbs-down" instead of class="icon-thumbs-up".
I have tried to achieve this using
<i class="<?php $file = file('include.txt');echo $file[n]; ?>"><strong> <?php $file = file('include.txt');echo $file[13]; ?></strong></i>
in order to change it on all pages.
However, this is not working. Thanks for any hints!
To clarify:
<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>
Content of include.text: 0.58% on line 1 -> All working fine. I got the thumbs up displayed and next to it the value 0.58%.
Now I tried to change to:
<i class="<?php $file = file('include.txt');echo $file[1]; ?>"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>
Content of include.text: 0.58% on line 1 and icon-thumbs-up on line 2. (Which I would like to change in the include.txt on a daily basis to icon-thumbs-up or icon-thumbs-down, depending on the value of line 1.)
If value from file('include.txt') is integer then you can use ternary operator to echo correct css class, eg. like this:
<li class="<?php echo (int) $file[0] < 0 ? 'icon-thumbs-down' : 'icon-thumbs-up'; ?>"></li>
This is a bit hard to reply to as your code isnt that clear. I can however hand you the following suggestion via an example:
$someInt = 10;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo positive
$someInt = -3;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo negative
This is a short if/else (ternary). To demonstrate how it works, this would be the same in a full syntax:
$someInt = -3;
if($someInt < 0){
echo 'icon-negative';
}
else{
echo 'icon-positive';
}
I would do the following:
<?php
// your logic here, move in another file if this gets bigger:
function percentage($nb) {
$file = file('include.txt');
return $file[$nb];
}
?>
<i class="<?php echo percentage(5) > 0 ? 'positive' : 'negative' ?>">
<strong>Text</strong>
</i>

URL variable shown on page [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying my best to describe this. I'd basically like to be able to have something like this in a url:
mydomain.com/?&name=Courtney
The name variable being Courtney. I would like to print it onto the page so when visited with the name=Courtney it'd state "Hello Courtney!" or "Hello Taylor!" Depending on who it is, if neither, don't display their name and just say "Hello!" I'm hoping someone here knows how to use a name in a url variable and is welling to share.
Simple in essence, but, I'm not sure how simple it is in code.
Directly from the PHP website:
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
if you want to check if the variable is available:
if (isset($_GET["name"])) {
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
} else {
echo 'Hello!';
}
<?php
if($_GET['name']){
echo "Hello " . $_GET['name'] . "!";
}else{
echo "Hello!";
}
?>
use
if (isset($_GET['name'])) {
echo "Hello ".$_GET['name']."!";
}
You could do it all in one line with a Ternary Operator
// Collect name if present //
$name = (isset($_GET['name'])) ? ' ' . ucwords(htmlspecialchars($_GET['name'])) : '';
// Echo output //
echo "Hello{$name}!";
// Output if name present //
Hello Courtney!
// Output if no name present //
Hello!
I'll provide a quick example. Complicated subject, simple code.
<?php
if(isset($_GET['name'])){
$name = $_GET['name'];
echo 'Hello ' . $name;
}
else{
echo 'No name supplied'
}
?>
if you want to use the current page url then after page load you can use
var address= document.URL
then extract the name (as url in your question is like 'mydomain.com/?&name=Courtney')
var name= address.substring(address.lastIndexOf('=') + 1)
if(name.length>0)
{alert("Hello "+name)}
else
alert("Hello")

Switch Statment Give Always The default [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I have a problem in my script in wampserver. It runs well. But in my hosting it doesn't display
the page which is named classed.php?cat=[category_Name_Example]
<?php header("Content-type: text/html; charset=utf-8");
?>
<?php include_once("analyticstracking.php") ?>
<?php
include 'includis/html_codes.php';
include 'includis/config.php';
$catID= mysql_real_escape_string($_GET['cat']);
switch ($catID)
{
case 'javascript' :
$catName = "javascript";
$PageTitle = "Javascript ";
$img = "img/javascript.png";
break;
case 'htmlandcss' :
$catName = "htmlandcss";
$PageTitle = "html ";
$img = "img/html2.png";
break;
default:header('location: /404');
}
if (!isset($catID)){
header ('Location 404.php');
}
if (empty($catID)){header ('Location 404.php');}
include 'includis/db.php';
?>
please help thanx :)
Most probable reason is that you do not have an open database connection while calling to this function. In this case mysql_real_escape_string just returns false.

how to search using a textfield? php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to search a whole news feed by inserting a word in my text field. I have no idea how to do this. Here is my code, so you know what I mean with it.
<form action="search.php" method="get">
<tr><th>search: </th><td><input type="text" name="search" value="{$word}"></td></tr>
</form>
How can I check if the word I inserted in the searchbar, exists somewhere in the news feed?
My news feed is used like this:
$xml=simplexml_load_file("newsfeed.xml");
foreach($xml->channel->item as $item) {
echo '<h1 class="title">' . $item->title . '</h1>';
echo '<p class="desc">'.$item->description."</p>";
}
I think you can use the function strpos.
Find the numeric position of the first occurrence of needle in the
haystack string.
Example:
$a = 'Long text to look into it.'
if (strpos($a, 'it') !== false)
echo 'true';
In your case, you can use strpos either to find the word in the item title or the item description:
$a = $_GET['search'];
foreach($xml->channel->item as $item) {
print_result = 0; // flag to know if the search is in the feed.
if (strpos($item->description, $a) !== false){
print_result = 1;
} // end if desc
if (strpos($item->title, $a) !== false){
print_result = 1;
} // end if title
if(print_result == 1){
echo '<h1 class="title">' . $item->title . '</h1>';
echo '<p class="desc">'.$item->description."</p>";
} //end if print results.
} // end foreach
I would make a foreach like you do it now:
And make kind of relevance points for each news item. Something like if it's in the title then it's more relevant and if it's at the beginning and so on.
Then you can write it into an array and sort it.
I hope you have something like an id in your newsfeed.xml for this.

Categories