SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Question Possible? Or Just too Complicated?

    I'm trying to setup a script that will generate a price for a particular product based on user inputs. In this case it is for a popcorn website that will allow the customer to create a custom tin with 3 different popcorns... creating a custom price. I have the script working just fine, but now it needs to be database driven... this is where I don't understand it.

    PHP Code:
    var strArraySnowmanSize = new Array(strPleaseSelect'[strArraySmallBowl1]Small Bowl:10.00','[strArrayMediumBowl1]Medium Bowl:15.00');
    var 
    strArrayChristmasTreeSize = new Array(strPleaseSelect'[strArraySmallBowl1]Small Bowl:10.00','[strArrayMediumBowl1]Medium Bowl:15.00');
    var 
    strArraySmallBowl1 = new Array(strPleaseSelect'[strArraySmallBowl2]Carmel Corn:12.00','[strArraySmallBowl2]Butter:10.00');
    var 
    strArrayMediumBowl1 = new Array(strPleaseSelect'[strArrayMediumBowl2]Carmel Corn:15.00','[strArrayMediumBowl2]Butter:13.00');
    var 
    strArraySmallBowl2 = new Array(strPleaseSelect'[strArraySmallBowl3]Carmel Corn:12.00','[strArraySmallBowl3]Butter:10.00');
    var 
    strArrayMediumBowl2 = new Array(strPleaseSelect'[strArrayMediumBowl3]Carmel Corn:15.00','[strArrayMediumBowl3]Butter:13.00');
    var 
    strArraySmallBowl3 = new Array(strPleaseSelect'Carmel Corn:12.00','Butter:10.00');
    var 
    strArrayMediumBowl3 = new Array(strPleaseSelect'Carmel Corn:15.00','Butter:13.00'); 
    This is the most complex part of the script. As you can see, I could easily pull the information from the database... but arranging that information is what I can't seem to figure out. Maybe I'm just not putting enough thought into it. Is something like this worth my time or is it just to weird to handle?

    I apprecaite the help.
    TAKE A WALK OUTSIDE YOUR MIND.

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Urm, that isn't PHP code.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    everywhere
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks to be Javascript from a quick view

  4. #4
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I should have explained myself further. That is javascript. But I need to make the values in there ie: SmallBowl, Butter, Carmel Corn... I need to make these values database driven. Can I mix the two languages?
    TAKE A WALK OUTSIDE YOUR MIND.

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is totally doable. :-)

    Have you checked out MySQL.com: Building a Database-Driven Web Site Using PHP and MySQL? Where do you sit in terms of knowing how to do what?

    Here's one way it might kind of end up looking:
    PHP Code:
    $results mysql_query($sql);
    while ( 
    $row mysql_fetch_array $results ) )
    {
      echo 
    "var strArraySnowmanSize = new Array(strPleaseSelect, '[strArray$row[productName]]$row[productName]:$row[price1]',
    '[strArray
    $row[productName2]]$row[productName2]:$row[price2]');

    Php parses all the funky stuff out before the visitor's browser sees it, so it will work just fine, nothing to stop you from mixing languages.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I'm working on this and can't seem to figure this one out...

    mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    This is the error that I'm getting in referece to line 50:

    PHP Code:
    while ($query mysql_fetch_array($row)) { 
    The more complete query:

    PHP Code:
    $query mysql_query("SELECT tin_id, tin_name, tin_price FROM build_tins");
    if (!
    $query) {
      echo(
    '</table>');
      die(
    '<p>Error retrieving information from database!<br />'.
          
    'Error: ' mysql_error() . '</p>');
    }
    while (
    $query mysql_fetch_array($row)) {
      
    $tin_id $row["tin_id"];
      
    $tin_name $row["tin_name"];
      
    $tin_price $row["tin_price"];
        
     echo (
    "  <option value='[strArray$tin_name]$tin_name:$tin_price'>$tin_name</option>
              <option value='[strArray
    $tin_name]$tin_name:$tin_price'>$tin_name</option>  ");
        

    Any ideas?
    TAKE A WALK OUTSIDE YOUR MIND.

  7. #7
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you transposed the resourse and the returned row ...

    $query is the resource ID from your query , $row is an array that mysql_fetch_* puts data into eg try this ..
    PHP Code:
    <?
    while ( $row mysql_fetch_array$query ) ) {
    ?>

  8. #8
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Thanks firepages... brainfreeze.

    I have gotten it all coded correctly... however there is a problem that I had not anticipated. This is a javascript program and I needed to intergrate PHP in there. The actaul javascript code resides in the head of the page. When I place the PHP there, it doesn't show up. What can I do to make the PHP work in the head of the page...?
    TAKE A WALK OUTSIDE YOUR MIND.

  9. #9
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind that all PHP is doing is sending text to the browser. If that text happens to be valid javascript, then the browser receives valid javascript and everything works. If the text you produce with PHP is not javascript then it won't work.

    So... what you (and everyone else who uses PHP, because everyone who uses PHP has this sort of problem at some point) do is to look at the text that PHP is generating and see how it is not what you want, find what is wrong. Then you change it. If you cannot spot the problem, or you do not know how to fix it, then come back here with examples of code and desired output. :-)
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


Bookmarks

Posting Permissions

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