473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Using variables in hrefs

Hi all,

I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>

I wouldn't think that this is the correct syntax, but the basic idea is
there.

TIA

Miki
Jul 17 '05 #1
8 1664
"Michelle" wrote:
Hi all,

I was wondering if I can incorporate a variable (actually a
calculation)
in an href statement such as:

print "<a href=’calendar.php?month = $month-1’>go
back a month</a>

I wouldn’t think that this is the correct syntax, but the basic
idea is
there.

TIA

Miki


You can include anything as long as you urlencode it.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-variable...ict146420.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=490142
Jul 17 '05 #2
"Michelle" <miki@spam_me.net> wrote in message
news:5F8_c.286880$eM2.83490@attbi_s51...
Hi all,

I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>

I wouldn't think that this is the correct syntax, but the basic idea is
there.

TIA

Miki


Try this:

print "<a href='calendar.php?month={$month-1}'>go back a month</a>";

.... or:

print "<a href='calendar.php?month=" . $month - 1 . "'>go back a month</a>";

- JP
Jul 17 '05 #3
*** Michelle escribió/wrote (Sat, 04 Sep 2004 01:13:37 GMT):
I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>


In a double-quote delimited string you have variable parsing, not
expression parsing. You'll have to use the dot operator to concatenate
strings:

"blah" . funct($foo) . "bla"

http://www.php.net/manual/en/languag...ors.string.php

--
--
-+ Álvaro G. Vicario - Burgos, Spain - ICQ 46788716
+- http://www.demogracia.com (la web de humor para mayores de 100 años)
++ «Sonríe, que te vamos a hacer una foto para la esquela»
--
Jul 17 '05 #4
"Michelle" <miki@spam_me.net> wrote in message
news:5F8_c.286880$eM2.83490@attbi_s51...
Hi all,

I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>

I wouldn't think that this is the correct syntax, but the basic idea is
there.


Formatted String Approach

echo sprintf('<a href="calendar.php?month=%s">Go back a month</a>' ,
$month - 1);

Template Approach

$url = '<a href="calendar.php?month=[month]">Go back a month</a>';
echo str_replace('[month]' , $month - 1 , $url);

Precalculated Approach

$prevmonth = $month - 1;
echo "<a href='calendar.php?month=$prevmonth'>go back a month</a>";
Take your pick
Jul 17 '05 #5

"Michelle" <miki@spam_me.net> ???????/???????? ? ???????? ?????????:
news:5F8_c.286880$eM2.83490@attbi_s51...
Hi all,

I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>

I wouldn't think that this is the correct syntax, but the basic idea is
there.

Why don't write just '<a href="calendar.php?month='.$month.'">go back a
month</a>

Jul 17 '05 #6
"Varavva Yevgen aka xa4anypu" <xa******@sakura.ua> wrote in message
news:ch***********@fortress.intercom.net.ua...

"Michelle" <miki@spam_me.net> ???????/???????? ? ???????? ?????????:
news:5F8_c.286880$eM2.83490@attbi_s51...
Hi all,

I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>

I wouldn't think that this is the correct syntax, but the basic idea is
there.

Why don't write just '<a href="calendar.php?month='.$month.'">go back a
month</a>


Because $month contains the number of the current month, and he wants the
link to go to the previous month.

- JP
Jul 17 '05 #7
In article <5F8_c.286880$eM2.83490@attbi_s51>, Michelle wrote:
Hi all,

I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>

I wouldn't think that this is the correct syntax, but the basic idea is
there.


And the manual is there to tell you what the right syntax is.

Btw, if $current_month == 1, then $previous_month = 12
and if $current_month = 12, then $next_month = 1

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #8
"Tim Van Wassenhove" <eu**@pi.be> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
news:2p************@uni-berlin.de...
In article <5F8_c.286880$eM2.83490@attbi_s51>, Michelle wrote:
Hi all,

I was wondering if I can incorporate a variable (actually a calculation)
in an href statement such as:

print "<a href='calendar.php?month = $month-1'>go back a month</a>

I wouldn't think that this is the correct syntax, but the basic idea is
there.


And the manual is there to tell you what the right syntax is.

Btw, if $current_month == 1, then $previous_month = 12
and if $current_month = 12, then $next_month = 1

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>

the easiest, but not the fastest, IMHO way is
if(date('m',time())!==1) $prev_month=date('m', time())-1;
else $prev_month=12;
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
3
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed...
17
by: Michael Hill | last post by:
I created an element on-the-fly using javascript like: myA=document.createElement("A"); myA.href="Javascript:acton(this)"; myA.className = "smallLink";...
1
by: tconti | last post by:
Howdy: I have 2 general questions regarding the use of XSLT to generate emails. We have several procecesses that use XSLT to generate emails. We frequently run into issues where a client...
17
by: Davíð Þórisson | last post by:
now in my web I have some global variables to be used in many different subpages, in the old ASP I simply loaded a variables.asp file into memory using the eval() function. Now I'd like to use XML...
0
by: Anthra Norell | last post by:
Hi, Martin, SE is a stream editor that does not introduce the overhead and complications of overkill parsing. See if it suits your needs: http://cheeseshop.python.org/pypi/SE/2.2%20beta ...
13
by: mark4asp | last post by:
When I write a url in xhtml, with an unencoded ampersand, like this: http://localhost:2063/Client/ViewReport.aspx?Ref=58&Type=SUMMARY the xhtml sytax checker correctly indicates an error,...
3
by: etabetapi | last post by:
I'm trying to build a simple gallery using some thumbnail images to load larger images and SWF files by swapping out the source hrefs. It works okay with images, but I'm having trouble doing the same...
15
by: r0g | last post by:
Hi There, I know you can use eval to dynamically generate the name of a function you may want to call. Can it (or some equivalent method) also be used to do the same thing for the variables of a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.