Announcement

Collapse
No announcement yet.

extreme beginner question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • extreme beginner question

    I want to create a DB to store several layout options, and call them from the dif pages on my site
    Things like bgcolor, linkcolor, alinkcolor, tableborder, tableheader, etc. Things like that, so that when I want to change my site around, I can just change the DB instead of all the files in my site, I of course have no idea how to do this.
    Would this be the right direction:
    Code:
    <?php
    $DB_Connect = mysql_connect("localhost","progtest_thetake","**********"); 
    
    $DB_Connect = mysql_select_db ("progtest_layout"); 
    
    $DB_Connect = "CREATE TABLE colors ( " .
           "bgcolor TEXT, " .
    	   "linkcolor TEXT, " .
    	   "alinkcolor TEXT, " .
    	   "tableborder TEXT, " .
    	   "tableheader TEXT, " .
           ")";
    if ( mysql_query($DB_Connect) ) {
      echo("<P>colors table successfully created!</P>");
    } else {
      echo("<P>Error creating colors table: " .
           mysql_error() . "</P>");}
    
    $result = mysql_query ("INSERT INTO colors (bgcolor, linkcolor, alinkcolor, tableborder, tableheader) 
                    VALUES ('$#cc9999', '$#66ffcc', '$#33cccc', '$#ff33ff', '$#999933')"); 
    if(!$result) 
    { 
       echo "<b>Something not added:</b> ", mysql_error(); 
       exit; 
    } 
    if($result) 
            { 
            mysql_close($connection); 
            print "Something added sucessfully!"; 
            } 
    ?>
    [Edited by thetakerfan on 08-13-2000 at 05:16 PM]

  • #2
    A good start, but there are some errors. Also, for a simple site color scheme, there's probably a better way to do it which I'll explain below.

    1) Instead of mysql_connect... (its better to use mysql_pconnect, since I take it that it caches db connections for faster connections)
    2) Don't use TEXT as a table field unless its a LOT of text. In your case, I'd use VARCHAR(10) which allows for a string up to ten characters in length.
    3) Drop that , (comma) after the last field which you're creating in the CREATE TABLE query.
    4) You don't want to prefix the hex color values with $, since they're not really variables, and thats the text you'll be inserting straight into the database.

    Other than that, the code looks good. However, I'd recommend simply stating the variables in a PHP file, since a database would be overkill. Simple create a colors.inc file, and in it, place:
    Code:
    <?
    $bgcolor = "#cc9999";
    $linkcolor = "#66ffcc";
    $alinkcolor = "#33cccc";
    $tableborder = "#ff33ff";
    $tableheader = "#999933";
    ?>
    Then, for the rest of your site, just be sure the pages are parsed for PHP, and you can do something like:
    Code:
    <?
    require "colors.inc";
    ?>
    <html>
    <head>
    </head>
    <body bgcolor="<? echo $bgcolor; ?>" linkcolor="<? echo $linkcolor; ?>" alinkcolor="<? echo $alinkcolor; ?>">
    </body>
    </html>
    Then just change colors.inc when you want to change your colors...
    Hope it helps!

    Comment


    • #3
      thanks for the help Stallion
      the color thing is just so I can familiarize myself with the basics, before I try anything major
      Once school starts up again I will be the webmaster for our school paper, and I plan on storing the articles in a DB, the colors DB is just so I can get a feel for things

      Comment


      • #4
        I get this error now
        Warning: Supplied argument is not a valid MySQL-Link resource in /home/progtest/public_html/layout.php on line 28
        Something added sucessfully!
        thats after it says the table was created, "mysql_close($DB_Connect);" is line 28

        Comment


        • #5
          I tried using what Ed posted earlier to pull data from a DB, but I get an error, this is my code
          Code:
          <?php
          $DB_Connect = mysql_pconnect("localhost","progtest_thetake","*******"); 
          $DB_Connect = mysql_select_db("progtest_layout");
          
          $query = "select bgcolor, linkcolor, vlinkcolor, tableborder, tableheader from colors";
          $result = mysql_query ($query);
          
          { global $result;
          
          ?>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
          <HTML>
          <HEAD>
          <TITLE></TITLE>
          </HEAD>
          
          <BODY BGCOLOR="<?php echo $result[bgcolor] ?>" LINK="<?php echo $result[linkcolor] ?>" VLINK="<?php echo $result[vlinkcolor] ?>">
          <table bgcolor="<?php echo $result[tableborder] ?>">
          	<tr bgcolor="<?php echo $result[tableheader] ?>">
          		<td>Test</td>
          	</tr>
          </table>
          <?php
          }
          ?>

          Comment


          • #6
            Code:
            <?php
            $DB_Connect = mysql_pconnect("localhost","progtest_thetake","*******"); 
            $DB_use = mysql_select_db("progtest_layout",$DB_Connect);
            
            $query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tableheader FROM colors";
            $result = mysql_fetch_array(mysql_query($query));
            
            ?>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
            <HTML>
            <HEAD>
            <TITLE></TITLE>
            </HEAD>
            
            <BODY BGCOLOR="<?php echo $result[bgcolor]; ?>" LINK="<?php echo $result[linkcolor]; ?>" VLINK="<?php echo $result[vlinkcolor]; ?>">
            <table bgcolor="<?php echo $result[tableborder] ?>">
            	<tr bgcolor="<?php echo $result[tableheader]; ?>">
            		<td>Test</td>
            	</tr>
            </table>
            [Edited by Ed Sullivan on 08-14-2000 at 01:20 AM]

            Comment


            • #7
              I'm getting this error
              Warning: Supplied argument is not a valid MySQL result resource in /home/progtest/public_html/testlayout.php on line 6
              which is what I was getting before, until I removed the mysql_fetch_array, if I remove "mysql_fetch_array" I simply get "Test", but no colors

              [I'm off to bed, I'll deal with this tomorrow]

              Comment


              • #8
                ok, ditch the fetch array part, and see if this works:
                Code:
                $result = $result[0];
                I have no clue if it will or not...

                Comment


                • #9
                  This is what I have now, but I still just get "Text", with no color

                  Code:
                  <?php
                  $DB_Connect = mysql_pconnect("localhost","progtest_thetake","[edit]oops[/edit]"); 
                  $DB_use = mysql_select_db("progtest_layout",$DB_Connect);
                  
                  $query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tableheader FROM colors";
                  $result = $result[0];
                  
                  ?>
                  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
                  <HTML>
                  <HEAD>
                  <TITLE></TITLE>
                  </HEAD>
                  
                  <BODY BGCOLOR="<?php echo $result[bgcolor]; ?>" LINK="<?php echo $result[linkcolor]; ?>" VLINK="<?php echo $result[vlinkcolor]; ?>">
                  <table bgcolor="<?php echo $result[tableborder] ?>">
                  	<tr bgcolor="<?php echo $result[tableheader]; ?>">
                  		<td>Test</td>
                  	</tr>
                  </table>
                  </body></html>
                  [Edited by thetakerfan on 08-14-2000 at 03:29 PM]

                  Comment


                  • #10
                    Code:
                    <?php
                    $DB_Connect = mysql_pconnect("localhost","progtest_thetake","*******"); 
                    $DB_use = mysql_select_db("progtest_layout",$DB_Connect);
                    
                    $query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tableheader FROM colors";
                    $results = mysql_query($query);
                    $result = $results[0];
                    
                    ?>
                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
                    <HTML>
                    <HEAD>
                    <TITLE></TITLE>
                    </HEAD>
                    
                    <BODY BGCOLOR="<?php echo $result[bgcolor]; ?>" LINK="<?php echo $result[linkcolor]; ?>" VLINK="<?php echo $result[vlinkcolor]; ?>">
                    <table bgcolor="<?php echo $result[tableborder] ?>">
                    	<tr bgcolor="<?php echo $result[tableheader]; ?>">
                    		<td>Test</td>
                    	</tr>
                    </table>
                    No clue if this'll work. I need to work more on not using John's MySQL class...

                    Comment


                    • #11
                      still no luck

                      Comment


                      • #12
                        anyone?

                        Comment


                        • #13
                          Code:
                          <?php
                          mysql_connect("localhost","progtest_thetake","*******"); 
                          mysql_select_db("progtest_layout");
                          
                          $query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tableheader FROM colors";
                          $results = mysql_query($query);
                          $result = mysql_fetch_array($results);
                          
                          ?>
                          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
                          <HTML>
                          <HEAD>
                          <TITLE></TITLE>
                          </HEAD>
                          
                          <BODY BGCOLOR="<?php echo $result['bgcolor']; ?>" LINK="<?php echo $result['linkcolor']; ?>" VLINK="<?php echo $result['vlinkcolor']; ?>">
                          <table bgcolor="<?php echo $result['tableborder'] ?>">
                          	<tr bgcolor="<?php echo $result['tableheader']; ?>">
                          		<td>Test</td>
                          	</tr>
                          </table>
                          Try that, if it doesn't work -- tell us what the error is.

                          Comment


                          • #14
                            "Warning: Supplied argument is not a valid MySQL result resource in /home/progtest/public_html/testlayout.php on line 7"
                            which would be in here "$result = mysql_fetch_array($results);"

                            Comment


                            • #15
                              That code should work. Check your table/field names to be sure that they're 100% correct.

                              Comment

                              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                              Working...
                              X