Page 1 of 2

putting dynamic links on my dynamic images [Solved]

Posted: Fri Aug 17, 2007 11:22 am
by cybergirl
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm doing a project that I am hazy on php, and I have to finish it, no matter what. 

I got this code:

Code: Select all

echo '<br /><br /><br />
<img src="http://3dtours.org/tours/tourimages/'. $row['logo'] .'" /><br />

		   '.$row['company'] ;
and I want to put links on it so I tried this:

Code: Select all

echo '<br /><br /><br />
		    "<a href=\"clients.php?feature_num=$_GET[feature_num]\">
			<img src="http://3dtours.org/tours/tourimages/'. $row['logo'] .'" />
			</a>";<br />

		   '.$row['company'] ;
I felt really mixed up about where the quotes should go and how I do this, but I TRIED.

It gives me this in the browser when I roll over the image:

http://domain.com/\"clients.php?company=$_GET[company]\"

but what I want is this:

http://"www.domain.com/clients.php?feature_num=2 in the browser

So can you just fix it up for me and I'll study it and learn from that? Because I'm goin back and forth on this forever and reading text books and forums and I don't quite get it yet, but when I SEE it I will!

-Deborah


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: putting dynamic links on my dynamic images

Posted: Fri Aug 17, 2007 11:45 am
by superdezign
cybergirl wrote:http://domain.com/"clients.php?company=$_GET[company]"

but what I want is this:

http://"www.domain.com/clients.php?feature_num=2 in the browser
Those are totally different links, resulting from totally different lines of code. One thing that you need to realize is the the $_GET array is just an array.. Nothing special about it, so treat it like a regular array. You still have to put quotations around the associative member names.

There are two correct ways to output data from an array as a part of an echo:

Code: Select all

echo 'Foo from the query string is ' . $_GET['foo'] . '.';
// OR
echo "Foo from the query string is {$_GET['foo']}.";

my response

Posted: Fri Aug 17, 2007 12:33 pm
by cybergirl
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi I hope I post this right, please excuse, I'll keep trying but I didn't comprehend the posting directions because it didn't say (at the link I was given), HOW to get the code into those boxes, as in what to put to make it do that. So this is what I read, so it should be correct. 

Anyway, I also don't quite understand the directions for my question, but here is my attempt. Could you please correct me where I'm wrong please, and show me how you did it, please could you?

So I'll try it. You wrote:

Code: Select all

<?php
echo 'Foo from the query string is ' . $_GET['foo'] . '.';
// OR
echo "Foo from the query string is {$_GET['foo']}.";

?>
and now I'm trying it like that

Code: Select all

<?php
           echo '<br /><br /><br />
                    "<a href=\"clients.php?feature_num={$_GET['feature_num']}.";\">
                        <img src="http://3dtours.org/tours/tourimages/'. $row['logo'] .'" />
                        </a>";<br />

                   '.$row['company'] ;


?>
How is that?

I'll try it in the browser and see how it works. I basically took what you said:

Code: Select all

"Foo from the query string is      {$_GET['foo']}."; 
 "<a href=\"clients.php?feature_num={$_GET['feature_num']}.";\">
right?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 17, 2007 12:39 pm
by cybergirl
Oh I see, sorry about the posting directions...I think it was dug deeper in that page, because I finally saw how it was done, and I'm truly sorry. When I took my advertising art course, they said that people are bombarded by too much information and signs, and they tend to miss a lot, so you have to be limited to few words, or else people end up missing things. At the time I was 10 years younger, and I still had the ability to keep on top of it all because younger people have more energy and it declines as you age, and since I've read so much in my lifetime, I guess I'm declining quicker!

I'm really sorry, and I apologize. I should have spotted it! :cry:

Re: my response

Posted: Fri Aug 17, 2007 12:45 pm
by superdezign
cybergirl wrote:

Code: Select all

<?php
           echo '<br /><br /><br />
                    "<a href="clients.php?feature_num={$_GET['feature_num']}.";">
                        <img src="http://3dtours.org/tours/tourimages/'. $row['logo'] .'" />
                        </a>";<br />

                   '.$row['company'] ;


?>
That's why the

Code: Select all

tags are important. Do you se the highlighting error? You can't use single quotation marks in that echo statement because it is completely surrounded by them. Also, why do you have a double quotation mark before your anchor tag? You're getting farther away from a solution... I think you need to take the time to stop and think about what it is that you are writing.

And a quick note, the inline way of putting the data into a string (the second way I gave you) only works inside of double quoted strings.

thanks

Posted: Fri Aug 17, 2007 1:00 pm
by cybergirl
Thanks, I really don't have a clue what I'm doing anymore, but I think what you said should help me if I keep on trying. You're right I have trouble with quotes, once things get more complex, I really get mixed up with them. I'll try. Thanks.

Posted: Fri Aug 17, 2007 1:02 pm
by miro_igov
If you have troubles with quotes/braces you should use some code highlighting editor and after few days of coding you will quickly find qote/brace issues.

Posted: Fri Aug 17, 2007 1:03 pm
by superdezign
A good approach is to write exactly what you want manually, echo it to make sure it's what you want, then slowly replace the hard-coded values with variables. This is the sort of approach that helps with debugging anything, even far beyond echoing strings, and is a good approach for staying organized and focused.

Make the result, then make it so that your code will produce the result. Give yourself a visual goal.

Posted: Fri Aug 17, 2007 1:54 pm
by John Cartwright
Lets do a couple examples to simplify your errors

Code: Select all

echo 'Using single quotes <br />';
//Using single quotes

echo "Using double quotes <br />";
//Using double quotes

//We don't need to escape different kinds of quotes when inside opposite quote type
echo 'Mixing "double" and single quotes';
//Mixing "double" and single quotes'

//We need to escape the single quotes inside this string or else php interprets it as terminating the string and causing a parse error
echo 'Escaping single quotes to use inside this \'string\''; 
//Escaping single quotes to use inside this 'string'
The fourth example is why you are recieving an error. Compare your strings with the one in that example.

hi

Posted: Fri Aug 17, 2007 8:26 pm
by cybergirl
Thanks very much! Very helpful! I will try what you said, and I think now it's starting to come together in my mind! Where do I get a highlighter? Anyway, I'll try it tomorrow and if I still feel stuck I'll be back but just letting you know, this was really the best help I've gotten in a long time from anything or one!

please help!!!

Posted: Mon Aug 20, 2007 10:58 am
by cybergirl
hi, I got no time left, I cant' spend all day doing this, I'm ten days away from a deadline, and only 20% done, and I've been working on this for a month and a half. I've tried all manner of things, and I really cannot figure this out. Here's the last thing I tried. I need to put it in double quotes obviously, to make it pass the variables, BUT I get errors no matter what I do. How do I fix this, because I tried all kinds of things, and nothing works, just nothing. I really tried.

Code: Select all

<?php
 
$sql_result = mysql_query($sql); 
 
if (mysql_numrows($sql_result)
 < 1){ 
     echo 'Sorry, no images br>'; 
} 
else{ 
// ok, loop through each row: 
     while($row = mysql_fetch_assoc($sql_result)){ 
         // print the HTML to display the images. 

		  	echo "<a href=\"tour01.php?tourid=$row['tourid']\">
			<br />
		   	<br />
		   	<br />
		   	<img src=\"tours/tourimages/'. $row[\'logo\'] .'\" /></a>";
     } 
}



?>

Posted: Mon Aug 20, 2007 11:02 am
by VladSun
When concatenating array variables to string you should use:

Code: Select all

echo "Bla-bla ".$array_var['key'];
or

Code: Select all

echo "Bla-bla {$array_var['key']}";
and not:

Code: Select all

echo "Bla-bla $array_var['key']";

Posted: Mon Aug 20, 2007 11:04 am
by feyd
Your quotes were a bit screwy

Code: Select all

<?php
 
$sql_result = mysql_query($sql);
 
if (mysql_numrows($sql_result) < 1){
     echo 'Sorry, no images <br />';
}
else{
// ok, loop through each row:
     while($row = mysql_fetch_assoc($sql_result)){
         // print the HTML to display the images.

                        echo '<a href="tour01.php?tourid='. htmlspecialchars($row['tourid']) . '">
                        <br />
                        <br />
                        <br />
                        <img src="tours/tourimages/' . htmlspecialchars($row['logo']) . '" /></a>';
     }
}



?>

Posted: Mon Aug 20, 2007 11:19 am
by CoderGoblin
In this instance I don't think you want to use htmlspecialchars but urlencode

In situations where I am using long pieces of HTML with few variables I often prefer to use the HEREDOC syntax.

Code: Select all

$tourid=urlencode($row['tourid']);
echo <<<EOIMAGE
 <a href="tour01.php?tourid={$tourid}">
  <br />
  <br />
  <br />
 <img src="tours/tourimages/{$row['logo']}" /></a>
EOIMAGE;
For the heredoc syntax no spaces may be after the echo <<<EOIMAGE and the EOIMAGE is just a unique tag so you can change it as you want. You also cannot put put in php commands hence the build of variables beforehand. More information can be found here

If the $row values are simple a-z and/or 0-9 characters (no € , or other characters) you could skip the urlencode and simply replace {$tourid} with {$row['tourid']}.

Posted: Mon Aug 20, 2007 11:44 am
by cybergirl
Hi,
SOLVED!

I found that the first one with htmlurlencode worked, and I guess the next one was good but it didn't do it. So this has been solved, and I'm REALLY happy! I can go on now! Thanks to bits! thanks you soooooooooooo much!

-Deborah :) :D :) :D :) :D :) :D