Cannot make a href link to work - php

I am trying to include pagination on a site and I encountered this problem.
I have the following line, but instead of index.php I must put index.php?view_vehicles.
This is the line:
echo "<a href='index.php?page=$next'>Next</a> ";
It's the first time I encounter this issue and I don't know how to make it work.

From comments:
"It must be something like this: <a href='index.php?view_vehicles?page=$next'>Next</a> but it's now working this way. – AndreiCT
As I stated, additional GET parameters require & after the first parameter, not 2x ?.
<a href='index.php?view_vehicles&page=$next'>Next</a>

You should concatenate the variable $next in the string you're echoing:
echo "<a href='index.php?page=".$next."'>Next</>";

Related

How can I fix this HREF issue

I am struggling with an issue which I need a little bit of help with, basically I have created a search bar and when the results come up I only want the 'Session 1' or 'Session 2' to be href'd. Not the whole thing. A photo below:
img http://gyazo.com/eb677171c15b075f1fb4137d28227b3a.png
I would just like the Sessions to be hrefed but the paragraphs just.
My code below:
echo "<p><a href='../pages/session.php</a>".$results['1']."'><h3>".$results['title']."</h3>".$results['text']."</‌​p>";
Linking to my DB
Note that is not all of my code just where I believe the issue lies.
You placed </a> slightly wrong. The correct solution would be like this:
echo "<p><a href='../pages/session.php'>{$results['1']}</a><h3>{$results['title']}</h3>{$results['text']}</‌​p>";
If you use double quotes, you do not need to concatenate variables into your string, they are also validate within the string.
UPDATE
To display only the headlines as links try this:
echo "<a href='../pages/session.php'><h3>{$results['title']}</h3></a><p>{$results['text']}</‌​p>";
You have missed a bracket in ur a tag.It is like <a href='whatever'>blah</a>
Do you just want to href $results['1']? Here you go.
echo "<p><a href='../pages/session.php'>".$results['1']."</a><h3>".$results['title']."</h3>".$results['text']."</‌​p>";

How do I echo forward slash in php

I want to echo forward slash in php as follows
<a class="submenu" href="<?php echo base_url('products').'/'.rawurlencode('Agarbatte/Candle');?>"
Agarbatte/Candle is not a directory but when i do this, href takes this as directory and give me the error page not found.
Any help will be appreciated.
A slash in the URL will always be treated as a directory separator. And if you replace it with something else you won't have a slash anymore. But that's most likely the easiest solution..
If you have a 1:1 mapping between the path in the URL and your filesystem you are out of luck. If your application uses a "routing layer" though, you can modify it to not treat the / as a separator when some criterium is met.
Just use 'urlencode' on the 'Agarbatte/Candle'. I do not see a special need for 'rawurlencode'.
Though you can use the urlencode() function as others have said, I recommend not getting into this habit. Assuming you have these products in a database, use the id and you'll never run into these issues.
For example if the Candle product has id of 6 in the database... the PHP should resolve to: <a class="submenu" href='products/6'>
Furthermore, there are some shortcuts you can take here which will help you in the long run.
Instead of <?php echo "something" ?> turn on php short tags in php.ini and use <?= "something" ?>
You don't need to call base_url every time you write a link. Use the <base> HTML tag appropriately: <base href="http://localhost/yourapp/"> in <head>
So... with that said after using 1 and 2:
<a href='products/<?= $id;?>'><?= $name_of_product ?></a>
how about using "Agarbatte-Candle" in the url then on the output page
$url_bit=string_replace('-','/',$url_bit);

How would I jump to a point in a page using the hash or pound symbol(#) with a get variable?

I found this tutorial that explains what I want to do using html but when I echo out the code with a get variable there is no affect to the page. I would use, for example, the following code:
echo "<a href='post.php?id=".$id."#Comments'>Click here to go to the comments</a>";
echo "<a title='Comments'>Comments</a>";
I presume the problem is to do with the get variable, so would I have to end it, some how, before using the # symbol?
The problem actually lies in your HTML, because the anchor should be parsed correctly by browsers regardless of the query string.
Page anchors use the name attribute instead of the title attribute:
<a name='Comments'>Comments</a>
You can also apply this to the id attribute of any element:
<h2 id='Comments'>Comments</h2>
To define jump-labels you have to set a name and/or id-attribute:
echo "<a title='Comments' name='Comments' id='Comments'>Comments</a>";

Show an Image if a PHP argument proves true

I barely know how to use PHP and I can't seem to make my code show an image if a condition proves true. This is the code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
}
?>
I tried writing this thinking it would work and it didn't:
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
Also I tried a code I found which started with the function: header() but it caused a tremendously long error, which said something like header already defined.
Thanks
You have used 'double quotes' incorrectly in the echo statement.
Try the following:
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg' alt='Preview not available' />"
Regards,
Mahendra Liya.
You should var_dump($first) to know what it contains
check if the condition is really getting true
and also put single quote inside the double quote.
if ($first=="#"){
echo 'yes it is true';
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
}
close the img tag
The part of the query string starting with # (so-called "hash") is not being sent to the server. That is, if your page is called like myblog.com/foo?bar=baz#quux, you php script will only receive myblog.com/foo?bar=baz. You need javascript if you want to handle urls with hashes.

extra mystery space in php code

I am trying to pass a variable frm php to a javascript function, however a space keeps getting appeneded, and I can not see how.
The relevant php code snippet:
<p>Click for full description </p>".$brand."
<p>DELETE
$brand is what I want to pass, and deleteRec is the name of the function.
At the end of the first line I am echoing out brand before the link to deleteRec, and it contains no space. In my test case, it is set to simply 'o'.
The link that is genereated for deleteRec however, clearly contains a space, and I don't know where it is coming from.
DELETE
Do var_dump($brand) and look closely - there's almost certainly a space in it!
In which case, you can guard against it with trim
$brand=trim($brand);
Change:
<p>DELETE
to:
<p>DELETE
and tell us how it goes.
Try do echo the following:
echo "--$brand--";
This way you'll be able to see if there are any spaces in the variable.
As a general matter of style, I would change second link from:
<a href=\"#\" onclick=\"
deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>
to:
<?php
$deleteRecArgs = "'$ARTICLE_NO', '$brand', '$pg', '$nextArticleNo'";
?>
DELETE
It's easier to read and maintain.

Categories