Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

PHP syntax help

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
blue-genie
Asked:
blue-genie
  • 9
  • 4
  • 4
  • +2
9 Solutions
 
shobinsunCommented:
<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
 
blue-genieAuthor Commented:
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
 
X-treemCommented:
to echo a link do
echo "<a href=\"./img/i.jpg\">Download</a>";
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
blue-genieAuthor Commented:
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
 
X-treemCommented:
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
 
Ray PaseurCommented:
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
 
blue-genieAuthor Commented:
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
 
X-treemCommented:
exclude the folder with pics in your rewrite module
0
 
blue-genieAuthor Commented:
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
 
X-treemCommented:
try RewriteRule ^(test/assets) - [L]
0
 
blue-genieAuthor Commented:
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
 
Ray PaseurCommented:
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
 
blue-genieAuthor Commented:
thanks ray - for now i have to do the php in 24hrs for the demo.

any idea on a quick fix for now?
0
 
rinfoCommented:
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
 
rinfoCommented:
sorry try this
<img src="assets/img/<?php  $product->id ?>.jpg  alt= <?php  $product->headline ?> />"
0
 
blue-genieAuthor Commented:
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
 
rinfoCommented:
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
 
rinfoCommented:
This is for all jpg files and not just i.jpg
0
 
blue-genieAuthor Commented:
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
 
blue-genieAuthor Commented:
hey guys i opted for a complete rewrite cause i couldn't get my head around it.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 9
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now