Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    Charleston, SC
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Echo command is plain text with SQL query.

    I have many websites that use some of the same content blocks and instead of manually adding the content to each website, I thought it would be a good idea to have the content stored in a database as to only have one copy instead of multiple. I've done this and it works great except for one issue which is the images that are in the article are sometimes left aligned and other times right aligned.

    My solution was to add the following code to the article's image CSS tag that is in the database and use a variable on each of the individual pages to add the custom classes to the image.

    Code:
    <p><img src="img/charleston.jpg" class="<?php echo $ImgClass01; ?>">Is it the delightful year-round climate? The almost-European feel of its downtown city streets? The overwhelming...</p>
    However, the webpage is only showing the text when viewing the source code and not using the variable. Almost anything is possible, but I'm not sure how to make this work.

    Here is the code on the page...

    Code:
        <?php
        $PageTitle = "Charleston, South Carolina | Local Towns";
        $PageDescription = "Charleston is rated the first most popular vacation destination in the United States, and it surely must rank in...";
    
        // 1. Create a database connection
        $link = mysql_connect ("localhost", "root", "");
        if (!$link) die("Could not connect: " . mysql_error());
    
        // 2. Select a database to use
        if (!mysql_select_db ("articleBank"))
          die("Problem with the database: " . mysql_error());
    
        // 3. Set up query for items to display
        $query = "SELECT article FROM `articles` WHERE ID = 1";
    
        // 4. Execute the query
        $result_set = mysql_query ($query);
    
        include ("theme/header.php");
    
        // value for the class within the article to be printed on the page
        $ImgClass01 = 'img-responsive img-rounded pull-right';
    
    //Start a while loop to process all the rows
    while ($row = mysql_fetch_assoc ($result_set))
    {
        $article = $row['article'];
        echo $article; 
    } //END WHILE
    
        // 5. Close Connection
        mysql_close();
    
        include ("theme/footer.php");
        ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    Here is the code on the page...
    Looks like a 'scope' issue. Can we have the complete code of that page?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    Charleston, SC
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I took out all the formatting from the header and footer so the code is not cluttered. Also I've been reading about security and that some of this code is deprecated as of PHP Version: 5.5 and not to use the die commands. Any addition help with that would also be appreciated.

    Code:
    <?php
    // 1. Create a database connection
    $link = mysql_connect ("localhost", "root", "");
    if (!$link) die("Could not connect: " . mysql_error());
    
    // 2. Select a database to use
    if (!mysql_select_db ("articleBank"))
    	die("Problem with the database: " . mysql_error());
    
    // 3. Set up query for items to display
    $query = "SELECT article FROM `articles` WHERE ID = 1";
    
    // 4. Execute the query
    $result_set = mysql_query ($query);
    ?>
    
    <!doctype html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    
    <?php
    // value for the class within the article to be printed on the page
    $ImgClass01 = 'img-responsive img-rounded pull-right';
    
    	//Start a while loop to process all the rows
    	while ($row = mysql_fetch_assoc ($result_set))
    	{
    		$article = $row['article'];
    		echo $article; 
    	} //END WHILE
    
    // 5. Close Connection
    mysql_close();
    ?>
    
    </body>
    </html>
    Here is the table as it only has the one content snippet.

    Code:
    -- phpMyAdmin SQL Dump
    -- version 4.0.4
    -- http://www.phpmyadmin.net
    --
    -- Host: localhost
    -- Generation Time: Feb 01, 2014 at 10:31 AM
    -- Server version: 5.6.12-log
    -- PHP Version: 5.4.16
    
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    
    --
    -- Database: `articlebank`
    --
    CREATE DATABASE IF NOT EXISTS `articlebank` DEFAULT CHARACTER SET armscii8 COLLATE armscii8_general_ci;
    USE `articlebank`;
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `articles`
    --
    
    CREATE TABLE IF NOT EXISTS `articles` (
      `ID` int(20) NOT NULL AUTO_INCREMENT,
      `Article` text NOT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=armscii8 AUTO_INCREMENT=2 ;
    
    --
    -- Dumping data for table `articles`
    --
    
    INSERT INTO `articles` (`ID`, `Article`) VALUES
    (1, '<p><img src="img/charleston-sc-bridge.jpg" width="300" height="200" class="<?php echo $ImgClass01; ?>">Is it the delightful year-round climate? The almost-European feel of its downtown city streets? The overwhelming amount of history lurking behind...</p>');
    
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,390
    Thanks
    11
    Thanked 295 Times in 294 Posts
    The PHP engine isn't going to cruise through your variable $article looking for "<?php" tags and do that processing. $article is just a variable like any other variable. You'd need to do a string replace yourself to make the tweak. You could search for "<?php echo $ImgClass01; ?>" or use some other delimiters for it.


  •  

    Tags for this Thread

    Posting Permissions

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