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")
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wrote this code
<html>
<head>
<title> page 1</title>
<body>
<style>
a{
margin-left:10px;
}
</style>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = 201102820" ;
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)){
if ($row["major"]=="computer engineerig"){
echo "welcome ". $row["name"];
echo '<img src="tran.png"/>';
}
}
?>
</body>
</html>
but when I run it show me like this
Welcome stephen ICON
the icon(picture) that I put comes in front of the text .
Can I do like this
welcome stephen
ICON
I want the icon (picture) comes under the text.
HTML have a tag called Break :) you should echo this :
echo "welcome ". $row["name"];
echo "<br />";
echo '<img src="tran.png"/>';
you need a line break
echo "welcome ". $row["name"] . '<br/>';
echo '<img src="tran.png"/>';
or
echo '</p>' . "welcome ". $row["name"] . '</p>';
echo '<img src="tran.png"/>';
<a> and <img> are inline elements by default. See this article : CSS display: inline vs inline-block
So you have to put the ICON block in a block element to see a separation between the name and the icon.
Like this for example :
if ($row["major"]=="computer engineerig")
{
echo "welcome ". $row["name"];
echo '<p><img src="tran.png"/></p>';
}
Or adding a <br/> like suggested #Noor Adnan
<?php
//here you can add php
?>
<p>welcome <?php echo $row["name"]; ?> </p>
<br />
<img src="tran.png"/>
<?php
// here you can add your php
?>
If you Separate your HTML and PHP then you can easily add css and HTML inside your PHP.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My code is below.
I set up XAMP, and whatever non-php stuff I type in the html file prints out just fine.
But where is the '000' coming from?
Looks vaguely like some UNIX permissions but again, what is going on?
my lessons mention nothing like this. Thanks.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OOP in PHP</title>
<?php include("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new person();
$jimmy = new person();
$stefan->set_name('Stefan Mischook');
$jimmy->set_name('Nick Waddles');
echo "Stefan's full name: " + $stefan->get_name();
echo "Nick's full name: " + $jimmy->get_name();
?>
</body>
</html>
and this...
<?php
class person {
var $name;
function __construct($persons_name) {
$this->name = $persons_name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
Use . for concatenating strings
echo "Stefan's full name: " . $stefan->get_name();
echo "Nick's full name: " . $jimmy->get_name();
+ is additon: 0 + 0 = 0. You wan't to concatenate. In PHP use .:
echo "Stefan's full name: " . $stefan->get_name();
echo "Nick's full name: " + $jimmy->get_name();
^---- mathematical addition
You're trying to add two strings as if they were numbers. You want ., for concatenation.
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!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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
It's been a long time since I have done php so sorry for the silly question.
This is my current code, I'm trying to save the URL as a variable so that I can insert it into the echo, but it doesn't seem to work as nothing appears:
<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>
<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
I have echoed $old_url and can see that it has the correct value, but how do I insert the value into the echo do_shortcode with url="$old_url"?
This doesn't work either:
<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
You'll need to switch your quotes around. Single quotes print everything out as-is. Double-quotes will process the variables. Also, echo is not needed within an echo.
<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>
Another way to do it without switching your quotes is to break out of the statement:
<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Variables are not replaced in single quotes ...
<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Singles quotes doesn't allow variables parsing.
For example :
$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"
So you have to use double quotes or use the concatenate operator : .
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.