Submit Your Article Forum Rules

Results 1 to 10 of 10

Thread: PHP coding help required

  1. #1

    PHP coding help required

    Hi all,

    Can anyone help me out with a bit of PHP? My web designer has gone awol and I am trying to get the site to show the alt text for images automatically from the product name. The code I am looking at is: -

    foreach (array_keys($products) as $key){
    $products[$key]["PICTURE"]='<a href="'.$cst_main_links["LINK_PRODUCT_DETAIL"]
    .'?ProductID='.$products[$key]["ProductID"].'" alt="'.$products[$key]["ProductName"].'" title="'.$products[$key]["ProductName"].'">'
    .GetProductThumb($products[$key]["ProductID"]).'</a>';


    In Firefox when I hover over the pictures the title attribute is working fine but when I right-click and look at the properties it says the alternate text is missing.

    Any help would be greatly appreciated.

  2. #2
    Junior Member
    Join Date
    Oct 2005
    Posts
    28
    What exactly does the generated HTML look like, can you paste it here? That PHP looks ok (not the way I'd do it but semantically is right), as long as the 2nd dimensional array key "ProductName" is meaningful and has a value associated with it.

    I assume it does though, since it is mentioned twice, and that's why I would like to see the HTML snippet of one of your images so that I can see if the title and the alt are somehow missing together or seperately.

  3. #3
    Hi doc,

    The generated html looks like: -

    [img]allthingsipod/productimages/thumb_12387.jpg[/img]

    Shouldn't the alt be after the img src tag rather than before it?

    Thanks

  4. #4
    Senior Member sands's Avatar
    Join Date
    Sep 2005
    Posts
    392
    You are right. The alt attribute is for images. So alt="Belkin Car/Home Audio Kit" should be within the <IMG> tag and not <A>.

  5. #5
    Thanks for the help Sands. Will try and edit it but PHP is not my strong point.

  6. #6
    Junior Member
    Join Date
    Oct 2005
    Posts
    28
    In this case, your new PHP from what you posted would look like:

    foreach (array_keys($products) as $key) {
    $products[$key]["PICTURE"] = '<a href="'.$cst_main_links["LINK_PRODUCT_DETAIL"]
    .'?ProductID='.$products[$key]["ProductID"].'" title="'.$products[$key]["ProductName"].'">'
    .GetProductThumb($products[$key]["ProductID"]).'</a>';
    }

    and then find the function GetProductThumb() and modify the writing of the <img /> tag in there. (you can post that code here also and I'll help).

  7. #7
    Hi doc,

    Hope this is the right code as it took me a while to find it.

    function GetProductThumb ($ProductID){
    $filepath=CST_IMAGE_DIRECTORY."/".CST_IMAGE_THUMB_PREFIX.$ProductID.".jpg";
    if (file_exists($filepath)){
    $image_size=getimagesize($filepath);
    $img='[img]'.CST_IMAGE_DIRECTORY_REL.'/'.CST_IMAGE_THUMB_PREFIX.$ProductID.'.jpg'.'[/img]';
    return ($img);
    }else{
    return '[img]'.CST_TEMPLATE_DIR.'/pagegfx/no-image-placeholder.gif[/img]';
    }


    Please let me know if you need any more info. Thanks again.

  8. #8
    Junior Member
    Join Date
    Oct 2005
    Posts
    28
    Hello again,

    Now what I'm pasting here should work, barring anything unforseen. One thing about scripting languages (and for that matter any software with updated versions) is that some things are dependent upon which version of PHP being run. I'm assuming here that you are running above PHP4. It probably won't matter regardless, I'm just saying it for indemnification.

    Code:
    foreach(array_keys($products) as $key) {
    	$products[$key]["PICTURE"] = ''.GetProductThumb($products[$key]["ProductID"], $products[$key]["ProductName"]).'';
    }
    
    function GetProductThumb ($ProductID, $alt=false) {
    	$filepath = CST_IMAGE_DIRECTORY."/".CST_IMAGE_THUMB_PREFIX.$ProductID.".jpg";
    	if(file_exists($filepath)) {
    		$image_size = getimagesize($filepath);
    		$img = '[img]'.CST_IMAGE_DIRECTORY_REL.'/'.CST_IMAGE_THUMB_PREFIX.$ProductID.'.jpg'.'[/img]';
    		return $img;
    	} else {
    		return '[img]'.CST_TEMPLATE_DIR.'/pagegfx/no-image-placeholder.gif[/img]';
    }
    I have included both functions because although there is a much more efficient way to go about it, that is the least amount of work on your part to get it working for now. If you have any other problems, I'll give them a shot.

  9. #9
    Hi doc,

    Sorry, you not found the cure. Made the changes you suggested but when I viewed the source of page there wasn't any alt text at all.

    In the function update you posted you put alt="'.$alt." , is this correct as I cannot see anything relating to .$alt? Please forgive me if I am asking stupid questions, as you can see PHP is not my forte.

    Thanks for your help so far.

  10. #10
    Junior Member
    Join Date
    Oct 2005
    Posts
    28
    hey there,

    unfortunately what i did should work and if it doesn't then it is not grabbing the alt text properly from the array it was passed earlier on in the function.

    what the $alt is doing is receiving its data from the foreach that calls GetProductThumb() - so in other words it says GetProductThumb(id, name) whereas 'name' => $products[$key]["ProductID"] is being passed as the actual alt tag.

    it is probably just that the specific alt tag in question is not in the associative two-dimensional array for the $key in question and is going to require some further, in-depth analysis from a PHP programmer looking directly at your code.

    the only thing i could do to troubleshoot would be to inside that foreach, right after the '</a>'; put:

    Code:
    print $products[$key]["ProductID"]."\n";
    and evaluate that wherever it outputs to the browser and then immediately destroy that line after you look at your live website so that it does not look funny. this will see if that actually contains a value or not (i'm assuming that it does not)

    sorry i couldn't be of more help.

Similar Threads

  1. Auto Coding
    By Canadave in forum Graphics & Design Discussion Forum
    Replies: 6
    Last Post: 03-24-2009, 10:41 PM
  2. Question on Coding
    By stevan in forum Other Engines/Directories
    Replies: 1
    Last Post: 05-28-2006, 11:07 PM
  3. Help with coding
    By jon_w_walker in forum Graphics & Design Discussion Forum
    Replies: 10
    Last Post: 11-21-2005, 10:39 AM
  4. Coding problems
    By michaelstevenson in forum Graphics & Design Discussion Forum
    Replies: 0
    Last Post: 11-11-2004, 10:19 AM
  5. Coding Question
    By stevan in forum Search Engine Optimization Forum
    Replies: 2
    Last Post: 10-21-2004, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •