Solved

PHP syntax help

Posted on 2012-04-13
20
284 Views
Last Modified: 2012-05-08
i'm using code from a tutorial to try and figure out whole bunch of things, but have a look at where it says download...

if the category is 3 - i want the data returned "img/i.jpg" for example to be returned as a download link... i can't figure out what the echo should be to do this.

<li class="product">
<img src="assets/img/<?php echo $product->id ?>.jpg" alt="<?php echo $product->headline ?>" />
<?php
$_currCat = $product->category;
if ($_currCat == "3")
{
      echo $product->headline ?> <i>
      <?php
      echo 'Download: ';
} else {
?>
<?php echo $product->headline ?> <i><?php echo $product->body?></i> <b><?php echo $product->dateAdded?></b>
<?php
}
?>
</li>
0
Comment
Question by:blue-genie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 4
  • +2
20 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 56 total points
ID: 37842272
<li class="product">
<img src="assets/img/<?php echo $product->id ?>.jpg" alt="<?php echo $product->headline ?>" />
<?php
$_currCat = $product->category;
if ($_currCat == "3")
{
     echo $product->headline ?>
     <?php
     echo 'Download: ';
} else {
?>
<?php echo $product->headline ?> <i><?php echo $product->body?></i> <b><?php echo $product->dateAdded?></b>
<?php
}
?>
</li>

You added one '<i>' extra.
0
 
LVL 39

Author Comment

by:blue-genie
ID: 37842338
hey no - i was just about to come and try explain better.

the first if ... is where i'm having the problem ...  - thanks for the extra <i> as well but
if
      echo 'Download: <a href=\"#\">'.$product->body.'</a>'  ;


$product->body returns assets/1.jpg

i want to echo that in such a way that it becomes a link that will open 1.jpg
so with the current example - it's higlighted like a link but if i click on it

http://localhost/%22#\"
0
 
LVL 8

Assisted Solution

by:X-treem
X-treem earned 168 total points
ID: 37842341
to echo a link do
echo "<a href=\"./img/i.jpg\">Download</a>";
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 39

Author Comment

by:blue-genie
ID: 37842575
can you explain something to me

if the previous link

http://localhost/test/

takes you to

http://localhost/test/#/test/?category=3
and i then have my link
echo "<a href=\"./img/1.jpg\">Download</a>"; //i'll hard code this for now

results in trying to load
http://localhost/test/#/assets/img/1.jpg
0
 
LVL 8

Assisted Solution

by:X-treem
X-treem earned 168 total points
ID: 37842649
what do you mean, is there a rewrite defined in htaccess?

just try it out first and see how you need to modify the download link
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 111 total points
ID: 37842667
Are you trying to cause the script to force a download of an image file?

You may be past this point, but just looking at the code snippet posted with the original question, I think something like this would be simpler and less prone to syntax errors (instead of all that back and forth between PHP and HTML).  

See http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
// SET A VARIABLE FOR USE IN A HEREDOC STRING
if ($product->category == "3")
{
    $ph = $product->headline;
}
else
{
    $ph = 'Download';
}

// CONSTRUCT STRING WITH HEREDOC NOTATION
$li = <<<HEREDOC
<li class="product">
<img src="assets/img/$product->id.jpg" alt="$product->headline" />
$ph
$product->headline<i>$product->body</i> <b>$product->dateAdded</b>
</li>
HEREDOC;

// WRITE THE STRING TO THE BROWSER
echo $li;

Open in new window

0
 
LVL 39

Author Comment

by:blue-genie
ID: 37842785
my LoadModule rewrite_module modules/mod_rewrite.so is enabled in httpd.conf

eventually i'm going to have to change that to load the link target dynamically - but what i'm questioning right now is the fact that the # is making the hardcoded example not load.

I don't know how else to explain it.
0
 
LVL 8

Expert Comment

by:X-treem
ID: 37842798
exclude the folder with pics in your rewrite module
0
 
LVL 39

Author Comment

by:blue-genie
ID: 37842959
ok i'm no xampp / php expert - not even close

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?main\-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub\-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub-folder/$1
RewriteCond %{HTTP_HOST} ^(www.)?main\-domain.com$ [NC]
RewriteRule ^(/)?$ sub-folder/index.php [L]

http://chanon-srithongsook.info/development-notes/change-main-domain-to-subfolder/

assume folder structure as localhost/test/assets/img/1.jpg

localhost/test/index.php ....

what do i need to do to make the link work?
0
 
LVL 8

Assisted Solution

by:X-treem
X-treem earned 168 total points
ID: 37843000
try RewriteRule ^(test/assets) - [L]
0
 
LVL 39

Author Comment

by:blue-genie
ID: 37843147
oh, Ray - missed your post in the middle there.

all i'm trying to do is use some existing code from a tutorial to put together a demo.
the original code simply pulls images an text back and displays it.
now i've added a category type - which if it's 3 - i want what's returned from the database (which will be a url to a document) - to be a link, which the user can then click on to download or view.

i'm thinking maybe the original tutorial files i'm using is too advanced for my php/jquery skill set.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 111 total points
ID: 37843606
If you want a good, structured learning path that will take you from zero-to-sixty in PHP, consider getting this book and giving yourself a little while to work through the examples.  It's very readable with its own downloadable code library that you can copy and modify for your own use.  Now in its fourth printing it has been a permanent part of my professional library since Edition One.
http://www.sitepoint.com/books/phpmysql4/
0
 
LVL 39

Author Comment

by:blue-genie
ID: 37843933
thanks ray - for now i have to do the php in 24hrs for the demo.

any idea on a quick fix for now?
0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 165 total points
ID: 37846078
You need not use echo in the line
<img src="assets/img/<?php echo $product->id ?>.jpg" alt="<?php echo $product->headline ?>" />

change that to

<img src="assets/img/<?php  $product->id ?>.jpg" alt="<?php  $product->headline ?>" />
0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 165 total points
ID: 37846091
sorry try this
<img src="assets/img/<?php  $product->id ?>.jpg  alt= <?php  $product->headline ?> />"
0
 
LVL 39

Author Comment

by:blue-genie
ID: 37851425
hi rinfo - thanks for that heads up on not using the echo -  but i'm specifically trying to figure out how to make the file name a link which the end user would be able to click to download the document.

tx
0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 165 total points
ID: 37851518
Link to download file would be as mentioned earlier
"<a href=\"./img/i.jpg\">Download</a>"
Just add the following to  .htaccess
<FilesMatch "\.(?i:jpg|odf|pdf|rtf|txt)$">
  Header set Content-Disposition attachment
</FilesMatch>
This will force files having .jpg,.pdf,.rtf,.odf .txt files to download instead of opening
on the browser.
And when user click on the link it will download file for them.
You may remove extension that you do not need
<FilesMatch "\.(?i:jpg)$">    //if you just have jpg files
0
 
LVL 9

Expert Comment

by:rinfo
ID: 37851526
This is for all jpg files and not just i.jpg
0
 
LVL 39

Author Comment

by:blue-genie
ID: 37861145
ok pardon my ignorance...

if i loop through a result set and create a link how does it equate to

"<a href=\"./img/i.jpg\">Download</a>"
0
 
LVL 39

Author Closing Comment

by:blue-genie
ID: 37943115
hey guys i opted for a complete rewrite cause i couldn't get my head around it.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question