Solved

PHP syntax help

Posted on 2012-04-13
20
279 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
  • 9
  • 4
  • 4
  • +2
20 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 56 total points
Comment Utility
<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
Comment Utility
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
Comment Utility
to echo a link do
echo "<a href=\"./img/i.jpg\">Download</a>";
0
 
LVL 39

Author Comment

by:blue-genie
Comment Utility
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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 111 total points
Comment Utility
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
Comment Utility
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
Comment Utility
exclude the folder with pics in your rewrite module
0
 
LVL 39

Author Comment

by:blue-genie
Comment Utility
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
Comment Utility
try RewriteRule ^(test/assets) - [L]
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 39

Author Comment

by:blue-genie
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 111 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
sorry try this
<img src="assets/img/<?php  $product->id ?>.jpg  alt= <?php  $product->headline ?> />"
0
 
LVL 39

Author Comment

by:blue-genie
Comment Utility
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
Comment Utility
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
Comment Utility
This is for all jpg files and not just i.jpg
0
 
LVL 39

Author Comment

by:blue-genie
Comment Utility
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
Comment Utility
hey guys i opted for a complete rewrite cause i couldn't get my head around it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now