E
emetib

  • Jan 24, 2012
  • Joined Oct 24, 2010
  • Oh I am stumped here!

    I am building a new theme in Wordpress using a BLANK-Theme using Windows xampp.

    Built new page home.php and copied index.php over to home.php then added html from my mockup. All appeared fine including images which I have placed directly in htdocs wp folder.

    Products/P20obverse.PNG

    Built new page and copied index.php over to products.php, edited for correct content, then in WP created page titled My Products and added 4 custom fields. Coded products.php to extract the values and all appear fine except the image value shows nothing.

    Copied images from wp main folder to BLANK-Theme/images/Products
    still no image appears nor does a broken image icon.

    Custom Field
    Name=image_test
    Value=images/Products/P20obverse.PNG

    Tested php code using echo $image and it prints the custom field value out correctly as

    images/Products/P20obverse.PNG

    ... yet I cannot get an image to appear on my products.php.

    Opened product.php in browser and copied link location of the broken image link.

    file:///C:/xampp/htdocs/mydomain/wp-content/themes/BLANK-Theme/%3C?php%20echo%20get_post_meta($post-%3EID,%20%27image_test%27,%20true);%20?%3E

    <?php
    
    /* 
    
    	Template Name: Product Page
    
    */
    
    ?>
    
    <?php get_header(); the_post(); ?>
    
    <div id='main-content'>
    
    	<h1> Buy</h1>
    <h2 class="product-title"><?php the_title(); ?></h2>	
    
    
    <div class="product-box">
            <img class="image" src="<?php echo get_post_meta($post->ID, 'image_test ', true); ?>" />  
    
    		<ul>
    
            <li><h5>Price</h5> $<?php echo get_post_meta($post->ID, 'price', true); ?></li>
            <li><h5>Product Code</h5> <?php echo get_post_meta($post->ID, 'product_code', true); ?></li>
            <li><h5>Dimensions</h5> <?php echo get_post_meta($post->ID, 'dimensions', true); ?></li>
            <li><a class="button" href="#">Add to Cart</a></li>
        </ul>
    </div>
    
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
    
    
    
    </div> <!-- END main-content -->
    
    
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>
    

    Anyone here have a similar problem like this?

    • dagon;10968250 wrote:

      you could use array_walk in the case 'section' to apply it to $_POST['photo'], but there's no real benefit

      Thanks 🙂

      I read http://php.net/manual/en/function.array-walk.php

      If you have time would you post how I'd write and use that code?

      as I said ... I am unclear how to apply one function to another here.

      using

              foreach($tags as $tag)
              {
              $tag = change_tag($tag); 
      

      will only upper case my first tag in the array.

      array_walk may upper case each tag word.

      • NogDog;10968153 wrote:

        If I'm understanding the question correctly, all you should need to do is run the function being called through your change_tag() function, e.g.:

        $result = change_tag(your_function($function, $params));
        

        Hey NogDog

        Thanks

        Using

        $tag = change_tag($tag);
        

        at the beginning

                foreach($tags as $tag)
                { 
                $tag = change_tag($tag);
        

        works

        Yet I'd like to use an ucfirst function in my controller switch rather than inserting it in to each function that has string input posted.

        • dagon;10967846 wrote:

          need to see the code your using.

          Thanks Dagon

          What additional code would you need to see other than the functions and switch I posted in my 1st post?

          • sneakyimp;10967782 wrote:

            The reason it prints nothing is because you have not defined $array to contain any values. $try it on $row. I still have no idea what you are trying to do.

            Hey sneakyimp,

            Thanks for the reply.

            I was off on a tangent yesterday and forgot to include my function to array code 🙂

            What I am attempting to do is basically create a function that can do selects and join with out all the entry data.

            I am expanding upon this code so I can do join/union.

            SO I need the column_name of all the tables I wish to work with ... so at this point ... data =' Input' ... and I NEED data = 'Input' join/union 'Input' ... which can be a simple or complex join with out all the SQL syntax code.

            
            function selectQuery($table, $cond="", $fields="",  $order="", $sort="asc", $limit="") 
                { 
                    $cond = ($cond=="")?"":"where $cond"; 
                    $fields = ($fields=="")?"*":$fields; 
                    $order = ($order=="")?"":"order by $order $sort"; 
                    $limit = ($limit=="")?"":"limit $limit"; 
                    $query = "select $fields from $table $cond $order $limit"; 
                    $result = mysql_query($query) or die(mysql_error()); 
                    $data = Array(); 
                    if(mysql_num_rows($result) > 0) 
                        while($row=mysql_fetch_array($result)) 
                            $data[] = $row; 
                    return $data; 
                } 
            

            I am basically having issues retrieving the column name from the database in an array so I can create a join tlb function that I can implement into the above code.

            I have not built many function codes so I thought this forum would be the best place to ask.

            • dagon;10967791 wrote:
              $tag=change_tag($tag);
              

              should be sufficient

              Hey dagon,

              Thanks for the reply.

              I thought it would be as you stated. I had placed

              $tag=change_tag($tag);
              

              before my post ... in the controller which is working with all my other functions but I didn't get the ucfirst to work. I placed it above and below the copy($_FILES .....
              which made scene to me since I am calling other functions there which place photo data into the DB.

              I also placed it at the beginning of the other two functions. So I thought I'd ask for some assistance.

              Right now all my functions are in db_fns.php which is an include at the top of index.php and index.php is my controller at the moment.

              • I want to use it on all tags inserted into the database.

                • I thought print_r($array); would help me troubleshoot this but it prints nothing.

                  function Tbl_fields_Array()
                  {
                  $connection = db_connect();
                  
                  
                  $query ="SELECT column_name FROM information_schema.columns WHERE table_name = 'posts' 
                  union 
                  SELECT column_name FROM information_schema.columns WHERE table_name = 'users'" or die(mysql_error()); 
                  
                  $result = mysql_query($query);
                  
                  if($result == false) 
                  {
                     user_error(mysql_error()); // get some debug info
                  }
                  $result = result_to_array($result);
                  
                  while ($count = 0; $row = mysql_fetch_array($result); $count++) 
                  {
                    echo $row['column_name'].'<br/>' . ' ' . $row['Type'].'<br/>';
                  
                  } 
                  }
                      echo "The name is " . $array[0];
                  	print_r($array);
                  
                  Tbl_fields_Array();	
                  
                  
                  
                  • I am unclear how to apply one function to another here.
                    I tried it in the switch with no results.
                    Also tried it directly in the other functions but I am just not getting it.

                    I have my ucfirst function

                    function change_tag($tag)
                    {
                    return ucfirst(strtolower($tag));
                    }
                    //print change_tag('HMM.'); 
                    

                    switch

                    	  case "create":
                    
                    		if (is_jpeg($_FILES['photo']['type']['file']) 
                    			  and is_valid_file_size($_FILES['photo']['size']['file'])
                    		   )
                    		{
                        if(is_fields_filled_out($_POST['photo']))
                    	    {
                    			  $_POST['photo']['theme_id'] = $this_weeks_theme['id'];
                    
                    				$photo_id = create_photo($_POST['photo']);
                    				copy($_FILES['photo']['tmp_name']['file'], './photos/original/'.$photo_id.'.jpg');
                    				 $notice = "Created photo";	
                    	    }
                    
                    
                    break;	
                    
                      }
                    

                    and I want to use change_tag with these functions

                    	function create_photo($params)
                    	{
                    	   db_connect();
                    
                    	 $query = sprintf("INSERT INTO photos set
                    		                             photos.title = '%s',
                    									 photos.email = '%s',
                    									 photos.discuss_url = '%s',
                    									 photos.theme_id = '%s',
                    									 photos.fullname = '%s',
                    									 created_at = NOW()",
                    									 mysql_real_escape_string($params['title']),
                    									 mysql_real_escape_string($params['email']),
                    									 mysql_real_escape_string($params['discuss_url']),
                    									 mysql_real_escape_string($params['theme_id']),
                    									 mysql_real_escape_string($params['fullname'])
                    									 );
                    
                    	 $result = mysql_query($query);
                    
                    	 if(!$result)
                    	 {
                    		  return false;
                    	 }
                    
                    	 $photo_id = mysql_insert_id();
                    
                    	 create_photo_tags($params['tags'], $photo_id);
                    
                    	 return $photo_id;
                    }	
                    
                    
                    
                    /**
                     * creates our tags for our photos
                     * @param string $tag_str
                     * @param int $id
                     * @return bool
                     */
                    function create_photo_tags($tag_str, $id)
                    {
                      db_connect();
                    
                    	$tags = explode(',', $tag_str);
                    
                    	foreach($tags as $tag)
                    	{
                    		 $not_empty = trim($tag);
                    		 if(!empty($not_empty))
                    		 {
                    		 $query = sprintf("SELECT id from tags WHERE name = '%s'",
                    			                                   mysql_real_escape_string(strip_tags(trim($tag)))
                    			                                   );
                    		  $result = mysql_query($query);
                    			if(!$result)
                    			{
                    				  false;
                    		  }
                    			$num_rows = mysql_num_rows($result);
                    
                    			if($num_rows > 0)
                    			{
                    			  $tag_id = mysql_result($result, 0, 'id');
                    			}
                    			else
                    			{
                    			  $query = sprintf("INSERT into tags set name = '%s'",
                    			                                   mysql_real_escape_string(strip_tags(trim($tag)))
                    			                                   );
                    		    $result = mysql_query($query);
                    			  if(!$result)
                    				{
                    				  false;
                    				}
                    			  $tag_id = mysql_insert_id();
                    			}
                    
                    			  $query = sprintf("INSERT into taggings set photo_id = '%s', tag_id = '%s'",
                    			                                    mysql_real_escape_string($id),
                    												mysql_real_escape_string($tag_id)
                    			                                   );
                    		    $result = mysql_query($query);
                       }
                    	}
                    
                    	return true;
                    
                    }
                    
                    
                    • sneakyimp;10967344 wrote:

                      [man]mysql_fetch_assoc[/man] or [man]mysql_fetch_array[/man] should be helpful for doing that. You might try using the [man]print_r[/man] function inside your loop to see what your query is returning.

                      As for your problem, it's not really clear what you are trying to do.

                      Hey sneakyimp,

                      Thanks for the reply

                      I have 2 tables.
                      With PHP I want to build an array of their column names.

                      This is working in Mysql and the result is
                      column_name
                      id
                      title
                      body
                      created
                      modified
                      created_at
                      user_id
                      username
                      password
                      email

                      using SQL code

                      SELECT column_name
                      FROM information_schema.columns
                      WHERE table_name = 'posts'
                      UNION
                      SELECT column_name
                      FROM information_schema.columns
                      WHERE table_name = 'users'
                      
                      • you have to start testing it.
                        Warning: mysql_num_rows() expects parameter 1 to be resource,

                        Usually means unable to execute the query.

                        So test

                        
                        $count=mysql_num_rows($result); 
                        echo 'POSTS: '.$count;
                        
                        $result = mysql_query($sql)
                         or die('Invalid query: ' . mysql_error());
                        

                        ]

                        • I'm thinking something like this

                          function Tbl_fields_Array(){
                          
                          $connection = db_connect();
                          
                          $TABLE_NAME = '';
                          
                          $result = mysql_query("SHOW FIELDS FROM $TABLE_NAME, $TABLE_NAME");
                          
                          $i = 0;
                          
                          while ($row = mysql_fetch_array($result)) {
                            echo $row['Field'] . ' ' . $row['Type'];
                          }
                          }
                          
                          • sry had just heard mom went to ER so my head was not in right place and could not delete my post once I clicked submit reply.
                            Didn't mean to hijack your thread.

                            NOW regarding Your 'Simple' login script ...

                            1st off have you created the files you will need?

                            A database with tables and filled it with some data?

                            Login form?

                            1. Create database
                            2. Create file main_login.php.
                            3. Create file checklogin.php.
                            4. Create file login_success.php.
                            5. Create file logout.php

                            MyDBTest_sql.php

                            <?php
                            $con = mysql_connect("localhost","peter","abc123");
                            if (!$con)
                              {
                              die('Could not connect: ' . mysql_error());
                              }
                            
                            if (mysql_query("CREATE DATABASE my_db_test",$con))
                              {
                              echo "Database created";
                              }
                            else
                              {
                              echo "Error creating database: " . mysql_error();
                              }
                            
                            mysql_close($con);
                            
                            
                            // Create table
                            mysql_select_db("my_db_test", $con);
                            $sql = CREATE TABLE `members` (
                              `id` int(11) NOT NULL auto_increment,
                              `username` varchar(25) NOT NULL default '',
                              `password` varchar(15) NOT NULL default '',
                              PRIMARY KEY (`id`)
                              ) TYPE=MyISAM AUTO_INCREMENT=2 ;
                            
                            -- 
                              -- Dumping data for table `members`
                              -- 
                            
                            INSERT INTO `members` VALUES (1, 'john', '1234');
                            
                            // Execute query
                            mysql_query($sql,$con);
                            
                            mysql_close($con);
                            ?> 
                            

                            main_login.php
                            Build your login in HTML form in this file.

                            I have not used Output Buffering With PHP so am not sure what you are attempting here.

                            I found a nice article on devshed that may assist you.
                            http://www.devshed.com/c/a/PHP/Output-Buffering-With-PHP/

                            and this tutorial
                            http://www.tutorialspalace.com/2010/07/php-login-script-tutorial/

                            • specifying that fields are arrays
                              I read
                              http://php.net/manual/en/function.mysql-field-table.php
                              and now am very confused.

                              I have 2 tables I'm working with which I'd like to include in a number of functions.

                              posts
                              ('post.id', 'title', 'body')

                              users
                              ('user_id', 'username', 'password')

                              I wish get the fields directly from the tables and specify that their fields are arrays.

                              your input is appreciated.

                              • I read
                                http://php.net/manual/en/function.mysql-field-table.php
                                and now am very confused.

                                I have 2 tables I'm working with which I'd like to include in a number of functions.

                                posts
                                ('post.id', 'title', 'body')

                                users
                                ('user_id', 'username', 'password')

                                I wish get the fields directly from the tables and specify that their fields are arrays.

                                your input is appreciated.

                                • RESOLVED

                                  Hey anakadote

                                  I replaced my index.php with the index.php I am using on my server and still had the same problem so I did a copy and paste of the text using note pad.

                                  NOW using ...

                                    case "create":
                                  
                                    //print_r($params['user']);
                                  
                                    print_r($params);
                                    break;
                                  

                                  I get what I need.

                                  Array ( [user] => Array ( [username] => User1 [password] => password ) [0] => sessions/create )

                                  I didn't see any difference in the index.php files yet Problem Solved.

                                  Thanks for your assistance!

                                  Oh and I found the TEST/DEBUG code I was asking about to test my code routes in case anyone needs it.

                                  I placed it in my index file like so ...

                                  		// if no route matched display error
                                  		if(!$route_match) { exit('no route found'); }
                                  print('controller: '.$route['controller'].' view: '.$route['view'].' id: '.$params['id']).'<br />';
                                  

                                  which gives

                                  controller: sessions view: create id:

                                  ' id: '.$params['id'] is blank right now because I am not connecting to the database yet to get
                                  user_id = '%s'

                                  cheers

                                  • well that prints

                                    Array ( [0] => sessions/create )

                                    • Hi anakadote

                                      Thanks for the reply!

                                      php code is printing out fine when finding posts $routes array
                                      http://localhost:8080/localFixers/posts

                                      but not when finding sessions $routes

                                      I am defining $params in index.php

                                      <?php
                                      
                                         // starts a session
                                      	 session_start();
                                      
                                      /**
                                      	 * dispatches route
                                      	 * @param array $routes
                                      	 * @return array
                                      	 */
                                      	include('config.php');
                                      
                                        function dispatcher($routes)
                                        {
                                           // Requested URL
                                      	  $url = $_SERVER['REQUEST_URI'];
                                      
                                        // Removes Apllication root from url
                                        $url = str_replace('/'.APP_ROOT.'/', '', $url);
                                      
                                      	// holds the named captures, $_POST data
                                        $params = parse_params();
                                      
                                      	// Removes query string from $url we don't need it anymore affect routes.
                                      	$url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $url);
                                      
                                      	//  becomes true if $route['url'] matches $url
                                        $route_match = false;	
                                      
                                      	// loops over $routes looking for a match
                                      	foreach($routes as $urls => $route)
                                        {
                                      		// if match found appends $matches to $params
                                      		// sets $route_match to true and also exits loop.
                                      	  if(preg_match($route['url'], $url, $matches))
                                      	  {
                                      		  $params = array_merge($params, $matches);
                                      		  $route_match = true;
                                      	    	break;
                                      	  }
                                        }
                                      
                                      	// if no route matched display error
                                      	if(!$route_match) { exit('no route found'); }
                                      
                                      	// include controller
                                        include(CONTROLLER_PATH.$route['controller'].'.php');
                                      
                                      	if(file_exists(VIEW_PATH.'layouts'.DS.$route['controller'].'.php'))
                                      	{
                                      			// includes controller layout
                                      	    include(VIEW_PATH.'layouts'.DS.$route['controller'].'.php');
                                      	}
                                      	else
                                      	{
                                      		// include default layout
                                      	  include(VIEW_PATH.'layouts'.DS.'application.php');
                                      	}
                                      
                                      	// reset our flashs back to empty.
                                        $_SESSION['flash']['notice'] = '';
                                      	$_SESSION['flash']['warning'] = '';
                                      
                                        }
                                      
                                      dispatcher($routes);
                                      
                                      /**
                                       * Return array of $_GET and $_POST data
                                       * @return array
                                       */
                                      function parse_params()
                                      {
                                        $params = array();
                                      
                                      	if (!empty($_POST))
                                      	{
                                      	  $params = array_merge($params, $_POST);
                                      	}
                                      
                                        if (!empty($_GET))
                                      	{
                                      	  $params = array_merge($params, $_GET);
                                      	}
                                      
                                      	return $params; 
                                      }
                                      
                                      ?>
                                      
                                      
                                      

                                      I am defining $routes in config.php

                                      
                                      <?php
                                      
                                      /**
                                       * Holds the configuration for app, CONSTANTS ect...
                                       * Good idea to look inside.
                                       * change the directory of your app in here
                                       * edit your routes in here.
                                       */
                                      
                                      
                                         // routes url to controller and view
                                      
                                      $routes = array(   
                                      				   // posts
                                      	               array('url' => '/^posts\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
                                      				   array('url' => '/^posts\/new$/', 'controller' => 'posts', 'view' => 'new'),
                                      				   array('url' => '/^posts\/create$/', 'controller' => 'posts', 'view' => 'create'),
                                      				   array('url' => '/^posts\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit'),
                                      				   array('url' => '/^posts\/(?P<id>\d+)\/update$/', 'controller' => 'posts', 'view' => 'update'),
                                      				   array('url' => '/^posts\/(?P<id>\d+)\/delete$/', 'controller' => 'posts', 'view' => 'delete'),
                                      
                                      
                                      				 	// homepage
                                      	               array('url' => '/^(posts)?\/?(page\/)?(?P<page>\d?)$/', 'controller' => 'posts', 'view' => 'index'),
                                      
                                      					// sessions
                                      					array('url' => '/^sessions\/new$/', 'controller' => 'sessions', 'view' => 'new'),
                                      					array('url' => '/^sessions\/create$/', 'controller' => 'sessions', 'view' => 'create'),
                                      					array('url' => '/^sessions\/delete$/', 'controller' => 'sessions', 'view' => 'delete')
                                      
                                      
                                      
                                      				);
                                      
                                      
                                      
                                      // The server root
                                      define('SERVER_ROOT', $_SERVER['DOCUMENT_ROOT']);	
                                      //echo $_SERVER['DOCUMENT_ROOT'].'<br />';
                                          // Directory structures
                                      	define('DS', '/');
                                      
                                      // Application Directory - *** change this to your app folder. *** case sensative
                                      define('APP_ROOT', 'localFixers');	
                                      //echo APP_ROOT;
                                          //address of website	*** change to http://localhost for virtual server ***
                                      	define('WEBSITE', 'http://localhost:8080/');
                                      
                                      // MVC paths
                                      define('CONTROLLER_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'controllers'.DS);
                                      define('MODEL_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'models'.DS);
                                      define('VIEW_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'views'.DS);
                                      
                                      //echo CONTROLLER_PATH.'<br />';
                                      //echo MODEL_PATH.'<br />';
                                      //echo VIEW_PATH.'<br />';
                                      
                                      
                                      //lib includes ...extends what files can do and shares with multiple applications
                                      include ('lib/database.php');
                                      include ('lib/controller.php');
                                      include ('lib/model.php');
                                      include ('lib/view.php');
                                      ?>
                                      
                                      
                                      • Setting up user login session code in MVC.

                                        I am NOT getting my populated array results printed to page.

                                        using ...

                                        controllers/sessions.php

                                        <?php
                                        
                                          include(MODEL_PATH.'user.php');
                                        
                                        
                                        switch ($route['view']) {
                                        
                                          case "new":
                                        
                                          break;
                                        
                                          case "create":
                                         echo OK; 
                                        
                                          print_r($params['user']);
                                        
                                          break;
                                        
                                        }
                                        
                                        ?>
                                        
                                        

                                        the page i am routing to (from sessions/new to sessions/create)
                                        http://localhost/localFixers/sessions/create

                                        echos ...
                                        OK
                                        for

                                          case "create":
                                         echo OK; 
                                        

                                        yet prints nothing for

                                        print_r($params['user']);
                                        

                                        I Did a var dump which shows the [user] Array is being populated.

                                        index.php

                                        <?php
                                        
                                          // starts a session
                                          	session_start();
                                        
                                          // Holds the configuration for app, CONSTANTS ect...
                                        	// Good idea to look inside.
                                        	// * change the directory of your app in here
                                        	// * edit your routes in here.
                                        	// * edit database params in here.
                                        	include('config.php');
                                        
                                        die('<pre>'.print_r(get_defined_vars(), true).'</pre>');
                                        
                                        
                                          function dispatcher($routes)
                                        
                                        AND SO ON for code
                                        

                                        Array
                                        (
                                        [GLOBALS] => Array
                                        (
                                        [GLOBALS] => Array
                                        RECURSION
                                        [_POST] => Array
                                        (
                                        [user] => Array
                                        (
                                        [username] => User1
                                        [password] => password
                                        )

                                                    )

                                        How do I TEST/DEBUG code to see why the populated Array
                                        is not displaying my array results on the page I have routes to?

                                        sessions/create

                                        should display

                                        Array ([username] => User1 [password] => password )

                                        • issue was the app root folder name on server

                                          /jobBoardTest/

                                          changed to /jobboardtest/

                                          all is working now 🙂