SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    An easyer way for this code?

    I have some code which im currently working on, but im sure there is an easyer way to write this. So if anyone knows how to shorten this please reply here. Thanks.

    PHP Code:
      $databases=mysql_query("SELECT Idtitle FROM friendop WHERE ID=$id");
      
    $databases1=mysql_query("SELECT Header FROM friendop WHERE ID=$id");
      
    $databases2=mysql_query("SELECT Footer FROM friendop WHERE ID=$id");
      
    $databases3=mysql_query("SELECT Sitename  FROM friendop WHERE ID=$id");
      
    $databases4=mysql_query("SELECT Siteurl FROM friendop WHERE ID=$id");
      
    $databases5=mysql_query("SELECT Color1 FROM friendop WHERE ID=$id");
      
    $databases6=mysql_query("SELECT Color2 FROM friendop WHERE ID=$id");
      
    $databases7=mysql_query("SELECT Color3 FROM friendop WHERE ID=$id");
      
    $databases8=mysql_query("SELECT Textcolor FROM friendop WHERE ID=$id");
      
    $databases9=mysql_query("SELECT Siteadmin FROM friendop WHERE ID=$id");
      
      
    $databases mysql_fetch_array($databases);
      
    $databases1 mysql_fetch_array($databases1);
      
    $databases2 mysql_fetch_array($databases2);
      
    $databases3 mysql_fetch_array($databases3);
      
    $databases4 mysql_fetch_array($databases4);
      
    $databases5 mysql_fetch_array($databases5);
      
    $databases6 mysql_fetch_array($databases6);
      
    $databases7 mysql_fetch_array($databases7);
      
    $databases8 mysql_fetch_array($databases8);
      
    $databases9 mysql_fetch_array($databases9);

      
    $idtitle $databases["Idtitle"];
      
    $header $databases1["Header"];
      
    $footer $databases2["Footer"];
      
    $sitename $databases3["Sitename"];
      
    $siteurl $databases4["Siteurl"];
      
    $color1 $databases5["Color1"];
      
    $color2 $databases6["Color2"];
      
    $color3 $databases7["Color3"];
      
    $textcolor $databases8["Textcolor"];
      
    $siteadmin $databases9["Siteadmin"];

      
    $idtitle htmlspecialchars($idtitle);
      
    $footer htmlspecialchars($footer);
      
    $header htmlspecialchars($header);
      
    $sitename htmlspecialchars($sitename);
      
    $siteurl htmlspecialchars($siteurl);
      
    $color1 htmlspecialchars($color1);
      
    $color2 htmlspecialchars($color2);
      
    $color3 htmlspecialchars($color3);
      
    $textcolor htmlspecialchars($textcolor);
      
    $siteadmin htmlspecialchars($siteadmin); 

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sure can:
    PHP Code:
    <?php
    $databases 
    mysql_query("SELECT * from friendop WHERE ID='$id'");
    $row mysql_fetch_array($databases);

    // now do the last 2 sections, but use $row[fieldname] for each one instead of all the different $database variables
    ?>
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  3. #3
    will code HTML for food Michel V's Avatar
    Join Date
    Sep 2000
    Location
    Corsica
    Posts
    552
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In php4 you can do this ( same code that Defender gave, but finished ) :

    PHP Code:
    <?php

    $databases 
    mysql_query("SELECT * from friendop WHERE ID='$id'");

    $row mysql_fetch_array($databases);

    $i=0;
    foreach(
    $r as $row) {
      
    $row2[$i] = htmlspecialchars($r);
      
    $i++;
    }

    extract($row2EXTR_OVERWRITE);

    ?>
    There must be an even shorter way to htmlspecialchars() them all, but for now I only see foreach

  4. #4
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The important thing to realize, is that you can select more than one field at a time from a DB. * means 'everything'.

    you could use commas to select only the fields you want...like this:

    PHP Code:
    SELECT headerfootersitename FROM friendop WHERE id=$id
    Hope this helps
    --There's my 1.5 cents, now where is my change!?!?

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea, well you can select all the rows and it won't matter.

    This isn't like a Oracle database that when you select something it will automatically display.
    he can select all (*) and choose what to display in his array $row. This saves time and coding, which is what he asked for
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  6. #6
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just tried this and it works.
    PHP Code:
      $databases=mysql_query("SELECT Idtitle,Header,Footer,Sitename,Siteurl,Color1,Color2,Color3,Textcolor,Siteadmin FROM friendop WHERE ID=$id");
      
      
    $databases mysql_fetch_array($databases);

      
    $idtitle $databases["Idtitle"];
      
    $header $databases["Header"];
      
    $footer $databases["Footer"];
      
    $sitename $databases["Sitename"];
      
    $siteurl $databases["Siteurl"];
      
    $color1 $databases["Color1"];
      
    $color2 $databases["Color2"];
      
    $color3 $databases["Color3"];
      
    $textcolor $databases["Textcolor"];
      
    $siteadmin $databases["Siteadmin"]; 
    Added these changes to the script here
    www.boxxnet.com/tellafriend1.zip

    Thanks guys.
    Last edited by Dean Wilson; Oct 20, 2001 at 22:35.


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
  •