+ Reply to Thread
Results 1 to 2 of 2

Thread: need help on php/html

  1. #1
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    need help on php/html

    trying to make my images line up right. The code works fine but i cant seem to get the price to go under the image instead of to the side of it and keeping the images all in same row.

    Code:
    $query2 = "SELECT * FROM game WHERE item_type = 'food' ORDER BY RAND() LIMIT 7";
    $result = mysql_query($query2);
     
    while($row = mysql_fetch_assoc($result)){
    $image = $row["item_img"];
    $desc = $row["item_desc"];
    $cost = $row["item_cost"];
    echo '<img src="Images/items/'. $image . '.png" alt='. $desc . ' />';
    echo '<br />' .$cost;
    }

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: need help on php/html

    Remember, HTML is about structure, not layout. Stop thinking "I need a new line here, so I'll put in a <br/>". First, come up with the structure, then style it. It looks like you're listing items, so using list elements is a natural fit.
    HTML Code:
    <ul class="items">
        <li><img src="..." alt="..."/>cost</li>
        ...
    </ul>
    Then comes styling:
    Code:
    .items, .items li {
        /* style reset */
        margin: 0;
        padding: 0;
        list-style-type: none;
    }
    .items li {
        float: left;
        text-align: center;
    }
    .items li img {
      display: block;
      height: 64px; /* or what-have-you */
    }
    Add padding, borders, background &c.

    To generate the item list, create a list view and pass in the query result.

    PHP Code:
    class Item {
      public 
    $item_cost$item_img$item_desc;
      function 
    __toString() {
        return 
    "<img src='{$this->item_img}' alt='{$this->item_desc}'/>{$this->item_cost}";
      }
    }

    ...

    function 
    listView($rslt$itemClass$listAttrs=''$itemAttrs='') {
      echo 
    "<ul $listAttrs>";
      while (
    $item $rslt->fetchObject($itemClass)) {
        echo 
    "<li $itemAttrs>$item</li>";
      }
      echo 
    '</ul>';
    }

    ...

    $db = new PDO('mysql:localhost;dbname=...', ...);
    $food $db->prepare("SELECT * FROM game WHERE item_type = 'food' ORDER BY RAND() LIMIT 7");
    $food->execute();
    listView($food'Item''class="food"'); 
    Further abstract the above code, wrapping the result in an Iterator and use that in the list view. You can also make the list view an object. You can also turn the $listAttrs and $itemAttrs parameters into arrays and create a function or a class to convert them into strings. Perhaps something like:
    PHP Code:
    class Attributes {
      private 
    $_data = array(), $_attrStr=null;
      function 
    __construct($data) {
        
    $this->_data $data;
      }
      function 
    __toString() {
        if (
    is_null($this->_attrStr)) {
            
    $this->_attrStr '';
            foreach (
    $this->_data as $name => $val) {
                
    $this->_attrStr .= $name='$val'";
            }
        }
        return 
    $this->_attrStr;
      }
    }

    ... 
    // everything else the same, except for:
    listView($food'Item', new Attributes(array('class' => 'food')); 
    You could also look into using one of the XML manipulation extensions to generate the HTML.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to 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
x10hosting free hosting for the masses
dedicated servers