SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help Simplifying Script (Replace numerals with variable?)

    I've begun storing articles and images in database tables. I think I've come up with a pretty good scheme for displaying images, but I'd like to simplify or condense it.

    First, consider a couple fields from my database table (gs_images_topics). The data below indicate there are three images (ImgN = Image Number) on a page displayed at MySite/Topics/State_Trees.

    Code:
    PostURL | ImgN
    State_Trees | 1
    State_Trees | 2
    State_Trees | 3
    Next, is the script I use to display an image if it exists, using the getimagesize function - and a lot of other stuff I've added, such as optional styling and captions.

    It's really the same script - or series of scripts - repeated over and over, one for each ImgN. For example, I could copy the last line in each section below and replace "3" with "4" if I need to display a fourth image on some pages. The scripts for Image #1 are a little different; as you can see, they don't include the numeral 1. However, I could and probably should change that.

    PHP Code:
     $imgDir '/images/'.$DIE1.'GS/Topics/'.$Section.'/';
     
    $imgDir2 '/images/'.$DIE2.'GS/Topics/'.$Section.'/';
     
    $imgDir3 '/images/'.$DIE3.'GS/Topics/'.$Section.'/';

     
    $imgPath $_SERVER['DOCUMENT_ROOT'] . $imgDir;
     
    $imgPath2 $_SERVER['DOCUMENT_ROOT'] . $imgDir2;
     
    $imgPath3 $_SERVER['DOCUMENT_ROOT'] . $imgDir3
    At the very end of the script I posted, you'll find three variables...
    $Img3L, $Img3C and $Img3R. These simply determine if an image should be centered (C) or floated to the left (L) or right (R). So I can insert an article in another database table that looks like this:

    Code:
    <p>First paragraph.</p>
    $Img2R
    <p>Second paragraph.</p>
    Then I do a str_replace on my display page...

    PHP Code:
    str_replace('$Img2R'$Img2R$text); 
    ...and the proper image displays.

    But does anyone know how I can condense my multiple scripts into a single script, so I can write something like $PathImg(Var), rather than $PathImg, $PathImg2, $PathImg3...$PathImg13, etc.?

    Thanks.

    PHP Code:
    <?php
    $ImageTopics 
    mysql_query("SELECT GIT.ID, GIT.PostURL, GIT.ImgN,
     GIT.ImgID, GIT.PathImg, GIT.Caption, GIT.Alt, GIT.Big, GIT.Online,
     GSImg.PathImg, GSImg.Name, GSImg.Source, GSImg.Big, GSImg.Online
     FROM gs_images_topics GIT
     LEFT JOIN gs_images GSImg ON GSImg.PathImg = GIT.PathImg
     WHERE PostURL = '
    $PostURL'") or die (mysql_error());

    while (
    $row mysql_fetch_array ($ImageTopics))
      {
    switch(
    $row['ImgN'])
    {
     case 
    1:
     
    $Caption1 str_replace('$BaseURL'$BaseURL$row['Caption']);
     
    $Alt1 $row['Alt'];
     
    $Big1 $row['Big'];
     
    $DIE1 $row['Online'];
      
    $PathImg1 $row['PathImg'];
      echo 
    '<br><br>$PathIMG1: ';
      echo 
    $PathImg1;
      echo 
    '<br><br>';
     break;
     case 
    2:
     
    $Caption2 str_replace('$BaseURL'$BaseURL$row['Caption']);
     
    $Alt2 $row['Alt'];
     
    $Big2 $row['Big'];
     
    $DIE2 $row['Online'];
      
    $PathImg2 $row['PathImg'];
      echo 
    '$PathIMG2: ';
      echo 
    $PathImg2;
     break;
     case 
    3:
     
    $Caption3 str_replace('$BaseURL'$BaseURL$row['Caption']);
     
    $Alt3 $row['Alt'];
     
    $Big3 $row['Big'];
     
    $DIE3 $row['Online'];
      
    $PathImg3 $row['PathImg'];
      echo 
    '$PathIMG3: ';
      echo 
    $PathImg3;
     break;
     default:
     break;
    }
    }

     
    $imgDir '/images/'.$DIE1.'GS/Topics/'.$Section.'/';
     
    $imgDir2 '/images/'.$DIE2.'GS/Topics/'.$Section.'/';
     
    $imgDir3 '/images/'.$DIE3.'GS/Topics/'.$Section.'/';

     
    $imgPath $_SERVER['DOCUMENT_ROOT'] . $imgDir;
     
    $imgPath2 $_SERVER['DOCUMENT_ROOT'] . $imgDir2;
     
    $imgPath3 $_SERVER['DOCUMENT_ROOT'] . $imgDir3;

    // $img = glob($imgPath . $PostURL . '1'.'*');
     
    $img2 glob($imgPath2 $PostURL '2'.'*');
     
    $img3 glob($imgPath3 $PostURL '3'.'*');

    // IMAGE #1
    if (empty($img))
    {
     
    // echo 'EMPTY';
    }
    else
    {
    foreach(
    $img as $pathImg){
        if(
    preg_match("[\.gif$|\.jpg$|\.png$]",$pathImg)){
            
    $image=$pathImg;
            break;
        }
    }

    if (
    file_exists($image)) {
        list(
    $width$height$type$attr) = getimagesize($image);
        
    $image=str_replace($_SERVER['DOCUMENT_ROOT'],"",$image);
       
    $image=str_replace("/home/geobear2/public_html","http://www.geobop.org",$image);

        
    $widthpx $width 'px';
        echo 
    "<div style=\"width:$widthpx;$StyleDiv1\" class=\"$ImgDivClass1\">";
        echo 
    "$BigLink1<img src=\"$image\" $attr$ImgClass1 alt=\"$Alt1\">$EndLink1<div$CaptionClass1>$DANGER1$Big1$Caption1</div></div>";
    }
    }

    // IMAGE #2
    if (empty($img2))
    {
     
    // echo 'EMPTY';
    }
    else
    {
    foreach(
    $img2 as $pathImg2){
        if(
    preg_match("[\.gif$|\.jpg$|\.png$]",$pathImg2)){
            
    $image2=$pathImg2;
            break;
        }
    }

    if (
    file_exists($image2)) {
        list(
    $width$height$type$attr) = getimagesize($image2);
        
    $image2=str_replace($_SERVER['DOCUMENT_ROOT'],"",$image2);
        
    $image2=str_replace("/home/geobear2/public_html","http://www.geobop.org",$image2);
        
    $widthpx $width 'px';

    $Img2Master '<div style="width:'.$widthpx.';"$ImgDivClass2>
     '
    .$BigLink2.'<img src="'.$image2.'" '.$attr.''.$ImgClass2.' alt="'.$Alt2.'">'.$EndLink2.'
     <div'
    .$CaptionClass2.'>'.$DANGER2.''.$Big2.''.$Caption2.'</div></div><br>';

    $Img2C str_replace('$ImgDivClass2'' class="imgcenter"'$Img2Master);
    $Img2L str_replace('$ImgDivClass2'' class="fleft"'$Img2Master);
    $Img2R str_replace('$ImgDivClass2'' class="fright"'$Img2Master);
    }
    }

    // IMAGE #3
    if (empty($img3))
    {
     
    // echo 'EMPTY';
    }
    else
    {
    foreach(
    $img3 as $pathImg3){
        if(
    preg_match("[\.gif$|\.jpg$|\.png$]",$pathImg3)){
            
    $image3=$pathImg3;
            break;
        }
    }

    if (
    file_exists($image3)) {
        list(
    $width$height$type$attr) = getimagesize($image3);
        
    $image3=str_replace($_SERVER['DOCUMENT_ROOT'],"",$image3);
        
    $image3=str_replace("/home/geobear2/public_html","http://www.geobop.org",$image3);
        
    $widthpx $width 'px';

    $Img3Master '<div style="width:'.$widthpx.';"$ImgDivClass3>
     '
    .$BigLink3.'<img src="'.$image3.'" '.$attr.''.$ImgClass3.' alt="'.$Alt3.'">'.$EndLink3.'
     <div'
    .$CaptionClass3.'>'.$DANGER3.''.$Big3.''.$Caption3.'</div></div><br>';

    $Img3C str_replace('$ImgDivClass3'' class="imgcenter"'$Img3Master);
    $Img3L str_replace('$ImgDivClass3'' class="fleft"'$Img3Master);
    $Img3R str_replace('$ImgDivClass3'' class="fright"'$Img3Master);
    }
    }
    ?>

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $sql 
    "SELECT

          GIT.ID
        , GIT.PostURL
        , GIT.ImgN
        , GIT.ImgID
        , GIT.PathImg   as gtPathImg
        , GIT.Caption
        , GIT.Alt
        , GIT.Big       as gtBig
        , GIT.Online    as gtOnline
        , GSImg.PathImg as giPathImg
        , GSImg.Name
        , GSImg.Source
        , GSImg.Big     as giBig
        , GSImg.Online  as giOnline

    FROM gs_images_topics GIT
         LEFT JOIN gs_images GSImg 
            ON GSImg.PathImg = GIT.PathImg
    WHERE PostURL = '
    $PostURL'";

    $ImageTopics mysql_query($sql) or die(mysql_error());

    $data = array();

    while (
    $row mysql_fetch_array ($ImageTopics)) {
        
        
    $row['Caption'] = str_replace('$BaseURL'$BaseURL$row['Caption']);
        
    $data[$row['ImgN'] = $row;

    }

    // $data will contain an array of the data in a structure simliar to:

    $data = array(
        
    => array(
            
    'ID' => '''PostURL' => '''ImgN' => '''ImgID' => '',
            
    'gtPathImg' => '''Caption' => '''Alt' => '''gtBig' => '',
            
    'gtOnline' => '''giPathImg' => '''Name' => '''Source' => '',
            
    'giBig' => '''giOnline' => ''
        
    ),

        
    => array(...),

        
    => array(...)
    );
    Take note of as after some of your column names some of them would conflict with each other.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks; I'll check that out after I straighten a few other things out.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your script revealed some flaws in my code/structure, so I made some changes, removing a few unnecessary fields, changing "gt" to ImgT (images_topics), etc.

    Also, I got an error on $data[$row['ImgN'] = $row; so I simply added another bracket - $data[$row['ImgN']] = $row; - which appears to be the correct fix.

    PHP Code:
    $sql "SELECT

          ImgT.PostURL
        , ImgT.ImgN
        , ImgT.PathImg AS ImgTPathImg
        , ImgT.Caption
        , ImgT.Alt
        , IMG.PathImg AS IMGPathImg
        , IMG.Source
        , IMG.Big
        , IMG.Online

    FROM gs_images_topics ImgT
      LEFT JOIN gs_images IMG 
      ON IMG.PathImg = ImgT.PathImg
    WHERE PostURL = '
    $PostURL'";

    $ImageTopics mysql_query($sql) or die(mysql_error());

    $data = array();

    while (
    $row mysql_fetch_array ($ImageTopics)) {
        
        
    $row['Caption'] = str_replace('$BaseURL'$BaseURL$row['Caption']);
        
    $data[$row['ImgN']] = $row;

    }

    // $data will contain an array of the data in a structure simliar to:

    $data = array(
        
    => array(
            
    'PostURL' => '''ImgN' => '''ImgTPathImg' => '',
            
    'Caption' => '''Alt' => '''IMGPathImg' => '''Source' => '',
            
    'Big' => '''Online' => ''
        
    ),

       
    // 2 => array(...),

       // 3 => array(...)
    ); 
    But I'm not sure how to plug in the arrays. For example, my original script (since modified) includes the following lines for images #2 and #3...

    PHP Code:
      $imgPath2 '/home/geobear2/public_html'.$PIQ2.'';
      
    $imgPath3 '/home/geobear2/public_html'.$PIQ3.''
    ...where $PIQ2 and $PIQ3 = the value stored in the field PathImage.

    To create values for $PIQ2 and $PIQ3, I simply used the number of each image stored in field ImgN to create a switch...

    PHP Code:
    switch($row['ImgN'])
    {
     case 
    2:
     
    $PIQ2 $ImageTopics['PI'];
     break;
     case 
    3:
     
    $PIQ3 $ImageTopics['PI'];
     break;
     default:
     break;

    ...where "PI" is the alias for ImgT.PathImage.

    So how can I use these arrays to replace the following code...

    PHP Code:
      $imgPath2 '/home/geobear2/public_html'.$PIQ2.'';
      
    $imgPath3 '/home/geobear2/public_html'.$PIQ3.''
    ...with a single line:

    PHP Code:
      $imgPath '/home/geobear2/public_html'.$PIQ.''
    ...that automatically displays the correct image path for image #2, the correct path for image #3, etc.?

    Thanks.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have another question. I thought it might be a solution from the beginning but forgot to mention it in my first post.

    Consider the following series of code and imagine additional lines replacing the numerals 2 or 3 with 4, 5, 6 and on and on...

    PHP Code:
     $imgPath2 $_SERVER['DOCUMENT_ROOT'] . $PIQ2;
     
    $imgPath3 $_SERVER['DOCUMENT_ROOT'] . $PIQ3;

     
    $img2 glob($imgPath2.'*');
     
    $img3 glob($imgPath3.'*'); 
    Now imagine condensing each series to a single line and replacing the numeral 2 or 3 with a variable, $Number...

    PHP Code:
     $imgPath'.$Number.' $_SERVER['DOCUMENT_ROOT'] . $PIQ'.$Number.';

     
    $img'.$Number.' glob($imgPath'.$Number.'.'*'); 
    This ought to work as long as every instance of $Number has the same value AND it somehow repeats itself if there are multiple values in the database field ImgN.

    For example, imagine we have three images in the datbase table...

    Code:
    ImgPath | ImgN
    images/one | 1
    images/two | 2
    images/three | 3
    Since there is a first image (1), every instance of $Number should = 1...

    PHP Code:
     $imgPath'.$Number.' $_SERVER['DOCUMENT_ROOT'] . $PIQ'.$Number.';

     
    $img'.$Number.' glob($imgPath'.$Number.'.'*'); 
    And since there's also a second image, the above script (combined with related scripts) should also replace $Number with 2, displaying a second image. Can this be done?

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well to access image one you would do this:
    PHP Code:
    print $data[1]['ImgTPathImg'];
    $data[1]['ImgTPathImg'] = 'new value'
    Time is a little low at the moment better post comming soon x.x
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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