Jump to content

Jenk

Members
  • Posts

    778
  • Joined

  • Last visited

    Never

Posts posted by Jenk

  1. The form:

    [code]<form action="page.php" method="post">
        <textarea name="mytextarea" cols="50" rows="5"></textarea>
        <br />
        <input type="submit" />
    </form>[/code]

    The php for page.php:
    [code]<?php
    $link = mysql_connect('host', 'user', 'pass');
    mysql_select_db('db', $link);


    if (!empty($_POST['mytextarea'])) {
        $sql = "INSERT INTO `table`(`column`) VALUES('" . mysql_real_escape_string($_POST['mytextarea']) . "'";
    } else {
        echo 'Input something you numpty!';
    }

    //mysql_query etc down here.

    ?>[/code]
  2. Dude, Think about it..

    Everytime a user clicks the add to basket button, your code need to add that product..

    So therefore you already know that you'll be inserting a record, or adding another item to an array, or writing another line to a flat file etc.

    Vice versa for removing the item.

    A very quick untested example:
    [code]<?php

    class Basket
    {
        private $userId;
        private $db;

        public function __construct (DataBaseClass $db, $userId)
        {
            $this->db = $db;
            $this->userId = (int) $userId;
        }

        public function addItem ($prodId, $quantity)
        {
            try
            {
                $this->db->parseQuery
                (
                    'REPLACE INTO `basket` (`userId`, `prodId`, `quantity`) '
                  .'VALUES(:1, :2)'
                  ,$prodId
                  ,$quantity
                );
                $this->db->commit();
            }
            catch (DataBaseException $e)
            {
                throw new BasketException('Error adding item');
            }
            catch (Exception $e)
            {
                throw $e;
            }
        }

        public function removeItem ($prodId, $quantity)
        {
            try
            {
                $this->db->parseQuery(
                    'SELECT `quantity` '
                  .'FROM `basket` '
                  .'WHERE `prodId` = :1 '
                  .'AND `userId` = :2'
                  ,$prodId
                  ,$this->userId
                );
                $this->db->commit();

                $quant = (int) $this->db->fetchField('quantity');

                if ($quant <= $quantity)
                {
                    $this->db->parseQuery
                    (
                        'DELETE FROM `basket` '
                      .'WHERE `prodId` = :1 '
                      .'AND `userId` = :2 '
                      ,$prodId
                      ,$this->userId
                    );
                }
                else
                {
                    $this->db->parseQuery
                    (
                        'UPDATE TABLE `basket` '
                      .'SET `quantity` = `quantity` - :1 '
                      .'WHERE `prodId` = :2 '
                      .'AND `userId` = :3'
                      ,$quantity
                      ,$prodId
                      ,$userId
                    );
                }
                $this->db->commit();
            }
            catch (DataBaseException $e)
            {
                throw new BasketException('Could not remove item.');
            }
            catch (Exception $e)
            {
                throw $e;
            }
        }
    }

    ?>[/code]
  3. [quote author=wildteen88 link=topic=106833.msg427692#msg427692 date=1157372216]
    Use
    [code=php:0]$calc = "\$rate = str_replace(array(\"L\",\"E\",\"F\",\"I\"), array(\$l, \$e, \$f, \$I), \$field1);";[/code]

    If you are using eval.

    However i see no use for eval here. Just use:
    [code=php:0]$rate = str_replace(array("L","E","F","I"), array($l, $e, $f, $I), $field1);[/code]
    [/quote]it appears that $field1 contains an equation, the OP is trying to replace Keywords with values, and then execute the equation. eval() will be needed if the OP wants that equation to execute in PHP :)
  4. dude.. you've been given an example. From that example you can tell what needs doing..

    [code]<?php

    if ($user->isLoggedIn()) {
        if ($user->isOperator()) {
            $image = 'pink with yellow pokadots';
        } else {
            $image = 'green'; //display green image
        }
    } else {
        $image = 'red'; //display red image
    }

    ?>[/code]
  5. Well you've got your $result array returned by the function (which you've stowed away in var $a)

    so with $a, assign to the template.

    the template then iterates and decorates the data, ala my previous example where I assumed Smarty.
  6. ok, well it looks like you are going majorly overkill with your arrays then.

    Instead of creating so many arrays and doing all the shennanigans with them, just extract from the DB what you need - the image source link and the text by the looks of it.

    Then assign that directly to your template engine, then have the template iterate over the values outputting the HTML.
  7. recursion is what you need, and a lot of patience.

    [code]<?php

    function filelist($dir)
    {
        if (!$dir = realpath($dir)) return null;

        static $files = array();
        static $dirs = array();

        $handle = opendir($dir);

        while (($file = readdir($handle)) !== false)
        {
            if (!in_array($file, array('.', '..')))
            {
                if (is_dir($path = ($dir . DIRECTORY_SEPARATOR . $file)))
                {
                    $dirs[] = $path;
                    filelist($path);
                } else {
                    $files[] = $path;
                }
            }
        }
       
        return array('dirs' => $dirs, 'files' => $files);
    }

    echo nl2br(print_r(filelist('.'), true));

    ?>[/code]
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.