SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    OK. I have an Ajax getproduct() function that calls getproduct.php page with the id of li tag elements from multiple uls in #sidebar div. The id of each li tag references a table in the database as '$q=$_GET["q"]'. All the rows in the tables called are the same and each contains a field called 'sold' which is empty. When The client purchases an item from a category / ul tag that only has one original item the database field for that item/sold has to be updated so that that item and row is echoed as sold!. The idea being that the item/row will not be removed from the database until confirmation or receipt of the item by the client. Other clients will be able to view the item sold but won't be able to purchase it for some time untill the purchaser receives the item.

    I'm guessing I'll have to add a new Ajax updateproduct() function that calls updateproduct.php. I've added 'Sold!' to the 'sold' field and can see the result echoed and have bookmarked the sql as 'updatesold' and can create the updateproduct.php query. The problem is that the echoed row of the sold item contains select quantity tag with options length 1 that still shows up as 1 when another client downloads the page as well as the button still having innerHTML of 'Buy Now!'. The purchasers select quantity options[0] is converted to 'Sold!' and that buttons innerHTML shows up as 'Sold!' but the next client won't because only the sold field is updated. I need to know if it is better to update the whole row in phpmyadmin and copy the new query to echo the complete update or try to catch the 'sold' innerText' clientside and alter the button and select quantity options from there? Any advice greatly appreciated. I might be wrong about needing a separatee updateproduct() function and updateproduct.php page and the conditions can be added to the original getproduct() function and getproduct.php? That would be a lot easier!:

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I use this technique on some sites.

    You let a customer put the item in the basket, but when there is only one left pop up a message that the item stock will need to be verified before confirming its availability. Now 2,6,10,1000 people can put the last item in their basket, the first to the checkout gets the item, the others are told on checkout that the item is no longer available and new stock will be in soon.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes Sorry, that won't work for me. I'm close to what I want with manipulation of the tables in phpmyadmin basicaly using the value of the sold field in the echoed results which would mean using an updateproduct function and an updateproduct.php. I'm trying to add a conditional if else after while rows fetch array?

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, but since your first post made very little sense to me I thought Id throw you another option.

    After reading again, Im still not sure I understand fully what your trying to do.

    Perhaps if you show us your Database schema it will be clearer. Sorry I cant be more help at this time with the info supplied.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Yeah Mandes, this is way to convaluted. Can you demonstrate a conditional if else after a while fetch array in php, something like this

    getproduct.php
    PHP Code:
    while($row mysql_fetch_array($result)) 

    if(
    $row.['sold']!==' ')//ie. updated field value = 'Sold!'
    {
    echo 
    "Sold";//should echo both types a lot more stuff, fieldset etc. HTML

    }
    if(
    $row.['sold']==' ')//ie. original field value is empty
    {
    echo 
    "Buy Now!" //should echo both types
    }

    Let me know if PHP will allow the conditions in the while loop for sure, so I'm not barking up the wrong tree. Thanks in advance.

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes you can put anything inside a while loop

    PHP Code:
    ......

    while(
    $row=mysql_fetch_array($results)){

     if (
    $row['field1'] == 'yes'){
      echo 
    'yes';
     }else{
      echo 
    'no';
     }

    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Right on Mandes will be trying this and might have to alter the js script as well as CSS but I should get what I'm looking for. I searched 'php if in while loop' and got this which demonstrates what you said
    http://php.about.com/od/learnphp/ss/php_loops.htm without the correct
    PHP Code:
    $row['field'
    syntax witch I thank you for. I'm wondering if I can echo both ifs and I'm probably over paranoid as it should do both.

    getproduct.php
    long but applicable
    PHP Code:
    <?php

    header
    ('Cache-Control: no-cache');
     
    $q=$_GET["q"];
     
    $link=mysql_connect("localhost","root","root")or die(mysql_error( ));
     
    mysql_select_db("bwi",$link) or die(mysql_error( ));
     
    $sql="SELECT SQL_CALC_FOUND_ROWS * FROM ".$q." LIMIT 0, 20";

    $result=mysql_query($sql);
     
    $sql="SELECT FOUND_ROWS() AS 'found_rows';";
     
    $rows=mysql_query($sql);
     
    $rows=mysql_fetch_assoc($rows);
     
    $rowsfound=$rows["found_rows"]; 
     
    while(
    $row mysql_fetch_array($result)) 


    if(
    $row['sold']==' ')
    {

    /*start rowcontainer div*/

    echo "<div id='rowcontainer" $row['id'] . "' class='rowcontainer' title='Scroll to See more'>";

    /*start rowcell div*/

    echo "<div id='rowcell" $row['id'] . "' class='rowcell'>";

    echo 
    "<input type='hidden' id='itemid" $row['id'] . "' value='row" $row['id'] . "'/>";

    /*start imagecell div*/

    echo "<div id='imagecell" $row['id'] . "' class='imagecell' align='center'>";

    echo 
    "<input type='hidden' name='imgref" $row['id'] . "' id='imgref" $row['id'] . "' class='imgref' value='" $row['href'] . "'/>";

    echo 
    "<img src='" $row['src'] . "' class='image' onclick='pictureclick()' title='Click to get a better look'/>";

    echo 
    "</div>";

    /*end imagecell div*/

    /*start descriptioncell div*/
     
    echo "<div id='descriptioncell" $row['id'] . "' class='descriptioncell'>";

    echo 
    "<h1>" $row['item'] . "</h1>";

    echo 
    "<p id='itemdescription" $row['id'] . "' class='itemdescription'>";

    echo 
    $row['description'] ."</br>" $row['sold'] . $row['list'] . "</p>";

    echo 
    "</div>";

    /*end descriptioncell div*/

    /*start ordercell div*/
     
    echo "<div id='ordercell" $row['id'] . "' class='ordercell'>";

    echo 
    "<fieldset id='addtoinvoice" $row['id'] . "' class='addtoinvoice'><br/>";

    echo 
    "<input type='hidden' name='code" $row['id'] . "' id='code" $row['id'] . "' class='code' value='" $row['code'] . "'/>";

    echo 
    "<input type='hidden' name='item" $row['id'] . "' id='item" $row['id'] . "' class=item' value='" $row['item'] . "'/>";

    echo 
    "<label for='color'>Color:</label>";

    echo 
    $row['color'] . "<br/><br/>";

    echo 
    "<label for='size'>Size:</label>";

    echo 
    $row['size'] . "<br/>";

    echo 
    "<input type='hidden' name='price" $row['id'] . "' id='price" $row['id'] . "' class='price' value='" $row['price'] . "'/><br/>";

    echo 
    "<label for='quantity'>Quantity:</label>";

    echo 
    $row['quantity'] . "<br/><br/>";

    echo 
    "<input type='hidden' name='vendor" $row['id'] . "' id='vendor" $row['id'] . "' value='" $row['vendor'] . "'/>";

    echo 
    "<input type='hidden' name='ref" $row['id'] . "' id='ref" $row['id'] . "' value='" $row['href'] . "'/>";

    echo 
    "<p class='backforward'>";

    echo 
    "<span class='row' >".$row['id']."</span>";

    echo 
    "&nbsp;";

    echo 
    "of";

    echo 
    "&nbsp;";

    echo 
    "<span class='rowsfound' >".$rowsfound."&nbsp;items</span>";

    echo 
    "</p>";

    echo 
    "<p class='bought'></p>";

    /*start buttoncontainer div*/

    echo "<div id='buttoncontainer" $row['id'] . "' class='buttoncontainer'>";

    echo 
    "<span id='value" $row['id'] . "' class='value'>" $row['price'] . "</span>";

    echo 
    "<button name='buynow" $row['id'] . "' id='buynow" $row['id'] . "' class='button' onclick='addtoinvoice()' type='button' title='Buy Now!'/>Buy Now!</button>";
    //change 'Buy Now!'  and may be class='button' to change onclick??
    echo "</div>";

    /*end buttoncontainer div*/

    echo "</fieldset>";

    echo 
    "</div>";

    /*end ordercell div*/

    echo "</div>";

    /*end rowcell div*/

    echo "</div>";

    /*end rowcontainer div*/


    }

    if(
    $row['sold']==' ')
    {

    /*start rowcontainer div*/

    echo "<div id='rowcontainer" $row['id'] . "' class='rowcontainer' title='Scroll to See more'>";

    /*start rowcell div*/

    echo "<div id='rowcell" $row['id'] . "' class='rowcell'>";

    echo 
    "<input type='hidden' id='itemid" $row['id'] . "' value='row" $row['id'] . "'/>";

    /*start imagecell div*/

    echo "<div id='imagecell" $row['id'] . "' class='imagecell' align='center'>";

    echo 
    "<input type='hidden' name='imgref" $row['id'] . "' id='imgref" $row['id'] . "' class='imgref' value='" $row['href'] . "'/>";

    echo 
    "<img src='" $row['src'] . "' class='image' onclick='pictureclick()' title='Click to get a better look'/>";

    echo 
    "</div>";

    /*end imagecell div*/

    /*start descriptioncell div*/
     
    echo "<div id='descriptioncell" $row['id'] . "' class='descriptioncell'>";

    echo 
    "<h1>" $row['item'] . "</h1>";

    echo 
    "<p id='itemdescription" $row['id'] . "' class='itemdescription'>";

    echo 
    $row['description'] ."</br>" $row['sold'] . $row['list'] . "</p>";

    echo 
    "</div>";

    /*end descriptioncell div*/

    /*start ordercell div*/
     
    echo "<div id='ordercell" $row['id'] . "' class='ordercell'>";

    echo 
    "<fieldset id='addtoinvoice" $row['id'] . "' class='addtoinvoice'><br/>";

    echo 
    "<input type='hidden' name='code" $row['id'] . "' id='code" $row['id'] . "' class='code' value='" $row['code'] . "'/>";

    echo 
    "<input type='hidden' name='item" $row['id'] . "' id='item" $row['id'] . "' class=item' value='" $row['item'] . "'/>";

    echo 
    "<label for='color'>Color:</label>";

    echo 
    $row['color'] . "<br/><br/>";

    echo 
    "<label for='size'>Size:</label>";

    echo 
    $row['size'] . "<br/>";

    echo 
    "<input type='hidden' name='price" $row['id'] . "' id='price" $row['id'] . "' class='price' value='" $row['price'] . "'/><br/>";

    echo 
    "<label for='quantity'>Quantity:</label>";

    echo 
    $row['quantity'] . "<br/><br/>";

    echo 
    "<input type='hidden' name='vendor" $row['id'] . "' id='vendor" $row['id'] . "' value='" $row['vendor'] . "'/>";

    echo 
    "<input type='hidden' name='ref" $row['id'] . "' id='ref" $row['id'] . "' value='" $row['href'] . "'/>";

    echo 
    "<p class='backforward'>";

    echo 
    "<span class='row' >".$row['id']."</span>";

    echo 
    "&nbsp;";

    echo 
    "of";

    echo 
    "&nbsp;";

    echo 
    "<span class='rowsfound' >".$rowsfound."&nbsp;items</span>";

    echo 
    "</p>";

    echo 
    "<p class='bought'></p>";

    /*start buttoncontainer div*/

    echo "<div id='buttoncontainer" $row['id'] . "' class='buttoncontainer'>";

    echo 
    "<span id='value" $row['id'] . "' class='value'>" $row['price'] . "</span>";

    echo 
    "<button name='buynow" $row['id'] . "' id='buynow" $row['id'] . "' class='button' onclick='addtoinvoice()' type='button' title='Buy Now!'/>" $row['sold'] ."</button>";
    //change
    echo "</div>";

    /*end buttoncontainer div*/

    echo "</fieldset>";

    echo 
    "</div>";

    /*end ordercell div*/

    echo "</div>";

    /*end rowcell div*/

    echo "</div>";

    /*end rowcontainer div*/


    }

    echo 
    "<link id='dognuts' rel='stylesheet' type='text/css' href='styles/product.css'/>";

    mysql_close($link);
    ?>
    Please let me know if any syntax is wrong. I'm going nuts trying to decide how I'm going to handle the change of onclick but it may as well be done server side.

  8. #8
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    ok the script above threw this error
    PHP Code:
    Parse errorsyntax errorunexpected $end in D:\use_ide_1\UniServer\www\getproduct.php on line 252 
    probably syntax error. will have to check it out latter!

  9. #9
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK please, before you go any further, please spend a few hours learning how to format your code, the code you posted will be totally unreadable by you just months after youve finished the project.

    Unless you are going to be referencing the html objects later, there is no need to give them a unique ID and NAME, that will cut down a lot on the coding you have currently.

    HTML can be written outside the <?php ?> tags to avoid constant use of echo statements.

    Honestly those few hours now learning to make your code more easily readable will save countless hours thoughout the rest of your career.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Unexpected $end is covered in the Common PHP Problems thread.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  11. #11
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Mandes & StarLion, thanks for the tips. I'll be able to express my requests better as soon as I get more comfortable with PHP.
    HTML can be written outside the <?php ?> tags to avoid constant use of echo statements.
    Could you show me an example of this using the getproduct.php format or
    PHP Code:
    $q=$_GET["q"]; 
    I've seen it before but never used it.

  12. #12
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Heinz Stapff View Post
    Mandes & StarLion, thanks for the tips. I'll be able to express my requests better as soon as I get more comfortable with PHP.

    Could you show me an example of this using the getproduct.php format
    http://www.webdeveloper.com/forum/sh...d.php?t=102731

    no need to reinvent the wheel, especially on Friday afternoon.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  13. #13
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Conditional echo on update field?

    OK. Guys, I didn't close the second if bracket. The error reported line pointed to an echo div end just before the one closing bracket I had on the last if. When I added the second bracket everything worked fine. I'd still like to know how to avoid the repetitive echo statements by placing HTML after ?> any direction greatly appreciated. Also, I use the more modern method of writing 'scripts' which helps us dyslexics overcome the closing bracket problem, ie.
    PHP Code:
    while($row=mysql_fetch_array($results)){

     if (
    $row['field1'] == 'yes'){
      echo 
    'yes';
     }else{
      echo 
    'no';
     }
    }

    //I would write

    while($row=mysql_fetch_array($results))
    {
    if (
    $row['field1'] == 'yes')
    {
    echo 
    'yes';
    }
    else
    // My case, another if
    {
    echo 
    'no';
    }

    Some how I can see the opening and closing better that way
    Also, I'm questioning the addition of 'ids' and class Names as well, since most of the JS/CLIENT SIDE is node oriented and get elements by id isn't really used much. Any suggestions about that would certainly be appreciated too!

  14. #14
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Heinz Stapff View Post
    I'd still like to know how to avoid the repetitive echo statements by placing HTML after ?> any direction greatly appreciated.

    you had
    PHP Code:
    echo "<p class='backforward'>"

    echo 
    "<span class='row' >".$row['id']."</span>"

    echo 
    "&nbsp;"

    echo 
    "of"

    echo 
    "&nbsp;"

    echo 
    "<span class='rowsfound' >".$rowsfound."&nbsp;items</span>"

    echo 
    "</p>"

    outside php tags becomes
    PHP Code:
    <?php
    ...more phpscript here
    ?>
    <p class='backforward'> 
     <span class='row' ><?php echo $row['id']; ?></span>
     &nbsp;of&nbsp
     <span class='rowsfound'><?php echo $rowsfound?>&nbsp;items</span> 
    </p>
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  15. #15
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes, thanks for that, I just saw that on other site though I don't see how it simplifies matters yet. The other thing I finally noticed was that only one of the ifs seems to be echoing correctly? abbreviated---
    PHP Code:
    while($row mysql_fetch_array($result)) 
    {
    if(
    $row['sold']==' ')
    {
    echo 
    "<button name='buynow" $row['id'] . "' id='buynow" $row['id'] . "' class='button' onclick='addtoinvoice()' type='button' title='Buy Now!'/>Buy Now!</button>";
    //Buy Now! doesn't show up as button innerText??
    }
    if(
    $row['sold']!==' ')
    {
    echo 
    "<button name='buynow" $row['id'] . "' id='buynow" $row['id'] . "' class='button' onclick='addtoinvoice()' type='button' title='Sold!'/>" $row['sold'] . "</button>";
    //Sold! does show up as button innerText??
    }

    Yeah I only saw the HTML output I was looking for? The other buttons show up as lines/CSS Borders only? The title shows up though with mouseover?
    Actually, the title shows up as 'Sold!' on all buttons?
    Last edited by Heinz Stapff; May 4, 2012 at 10:10. Reason: Actually, the title shows up as 'Sold!' on all buttons?

  16. #16
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes, It turns out I do need the
    PHP Code:
    $row['id'
    echoed as I have JS that is writen like so
    Code:
    document.getElementById('rowcontainer0').scrollIntoView(true);
    This Keeps throwing JS error: getElementById is null? I May be missing something in the while loop as only parts of the CSS is functioning right?

    Updated getproduct.php
    PHP Code:
    <?php

    header
    ('Cache-Control: no-cache');
     
    $q=$_GET["q"];
     
    $link=mysql_connect("localhost","root","root")or die(mysql_error( ));
     
    mysql_select_db("bwi",$link) or die(mysql_error( ));
     
    $sql="SELECT SQL_CALC_FOUND_ROWS * FROM ".$q." LIMIT 0, 20";

    $result=mysql_query($sql);
     
    $sql="SELECT FOUND_ROWS() AS 'found_rows';";
     
    $rows=mysql_query($sql);
     
    $rows=mysql_fetch_assoc($rows);
     
    $rowsfound=$rows["found_rows"]; 

    $buy=' ';
     
    while(
    $row mysql_fetch_array($result)) 
    {

    echo 
    $rows;
    //not sure this is right?

    }

    mysql_close($link);
    ?> 

    <div id="rowcontainer<?php echo $row['id']; ?>" class="rowcontainer" title="Scroll to See more">
    //I'm sure echo $row['id'];  is not right as it throws the error!?
    <div id="rowcell<?php echo $row['id']; ?>" class="rowcell">

    <input type="hidden" id="itemid<?php echo $row['id']; ?>" value="row<?php echo $row['id']; ?>"/>
    //not sure the value of a tag can be echoed this way either?
    <div id="imagecell<?php echo $row['id']; ?>" class="imagecell" align="center">

    <input type="hidden" name="imgref<?php echo $row['id']; ?> id="imgref"<?php echo $row['id']; ?>" class="imgref" value="<?php echo $row['href']; ?>"/>

    <img src="<?php echo $row['src']; ?>" class="image" onclick="pictureclick()" title="Click to get a better look"/>

    </div>
     
    <div id='descriptioncell<?php echo $row['id']; ?>" class="descriptioncell">

    <h1><?php echo $row['item']; ?></h1>

    <p id="itemdescription<?php echo $row['id']; ?>" class="itemdescription">

    <?php echo $row['description']; ?></br><?php echo $row['sold']; ?><?php echo $row['list']; ?></p>

    </div>
     
    <div id="ordercell<?php echo $row['id']; ?>" class="ordercell">

    <fieldset id="addtoinvoice<?php echo $row['id']; ?>" class="addtoinvoice"><br/>

    <input type="hidden" name="code<?php echo $row['id']; ?>" id="code<?php echo $row['id']; ?>" class="code" value="<?php echo $row['code']; ?>"/>

    <input type="hidden" name="item<?php echo $row['id']; ?>" id="item<?php echo $row['id']; ?>" class="item" value="<?php echo $row['item']; ?>"/>

    <label for='color'>Color:</label>

    <?php echo $row['color']; ?><br/><br/>

    <label for='size'>Size:</label>

    <?php echo $row['size']; ?><br/>

    <input type="hidden" name="price<?php echo $row['id']; ?>" id="price<?php echo $row['id']; ?>" class="price" value="<?php echo $row['price']; ?>"/>

    <br/>

    <label for="quantity">Quantity:</label>

    <?php echo $row['quantity']; ?><br/><br/>

    <input type="hidden" name="vendor<?php echo $row['id']; ?>" id="vendor<?php echo $row['id']; ?>" value="<?php echo $row['vendor']; ?>"/>

    <input type="hidden" name="ref<?php echo $row['id']; ?>" id="ref<?php echo $row['id']; ?>" value="<?php echo $row['href']; ?>"/>

    <p class="backforward">

    <span class="row"><?php echo $row['id']; ?></span>

    &nbsp;of&nbsp

    <span class="rowsfound"><?php echo $rowsfound?>&nbsp;items</span>

    </p>

    <p class="bought"></p>

    <div id="buttoncontainer<?php echo $row['id']; ?>" class="buttoncontainer">

    <span id="value<?php echo $row['id']; ?>" class="value" ><?php echo $row['price']; ?></span>

    <button name="buynow<?php echo $row['id']; ?>" id="buynow<?php echo $row['id']; ?>" class="button" onclick="addtoinvoice()" type="button" title="Buy Now!"/>Buy Now!</button>

    </div>

    </fieldset>

    </div>

    </div>

    </div>

    <link id="dognuts" rel="stylesheet" type="text/css" href="styles/product.css"/>
    //not sure CSS link is right?
    Sorry if this is too long. I need help on the whole thing

  17. #17
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Congrats, your code is looking much more better now, and easier to read :-)

    But for some reason youve removed all your html and everything outside your while loop ?? Thats why nothings working, the variables $row['xxx'] wont return anything once outside the while loop, your javascript is returning errors because the id tag its looking for doesnt exist.

    Try this

    PHP Code:
    <?php 
    header
    ('Cache-Control: no-cache'); 
     
    $q=$_GET["q"]; 
    $link=mysql_connect("localhost","root","root")or die(mysql_error( )); 
    mysql_select_db("bwi",$link) or die(mysql_error( )); 
    $sql="SELECT SQL_CALC_FOUND_ROWS * FROM ".$q." LIMIT 0, 20"
    $result=mysql_query($sql); 
    $sql="SELECT FOUND_ROWS() AS 'found_rows';"
    $rows=mysql_query($sql); 
    $rows=mysql_fetch_assoc($rows); 
    $rowsfound=$rows["found_rows"];  
    $buy=' '
    while(
    $row mysql_fetch_array($result))  
    // start while loop
    ?>

    <div id="rowcontainer<?php echo $row['id']; ?>" class="rowcontainer" title="Scroll to See more">  
    <div id="rowcell<?php echo $row['id']; ?>" class="rowcell"> 

    <input type="hidden" id="itemid<?php echo $row['id']; ?>" value="row<?php echo $row['id']; ?>"/>  
    <div id="imagecell<?php echo $row['id']; ?>" class="imagecell" align="center"> 

    <input type="hidden" name="imgref<?php echo $row['id']; ?> id="imgref"<?php echo $row['id']; ?>" class="imgref" value="<?php echo $row['href']; ?>"/> 

    <img src="<?php echo $row['src']; ?>" class="image" onclick="pictureclick()" title="Click to get a better look"/> 

    </div> 
      
    <div id='descriptioncell<?php echo $row['id']; ?>" class="descriptioncell"> 
     <h1><?php echo $row['item']; ?></h1> 
     <p id="itemdescription<?php echo $row['id']; ?>" class="itemdescription"> 
     <?php echo $row['description']; ?></br><?php echo $row['sold']; ?><?php echo $row['list']; ?></p> 
    </div> 
      
    <div id="ordercell<?php echo $row['id']; ?>" class="ordercell"> 
     <fieldset id="addtoinvoice<?php echo $row['id']; ?>" class="addtoinvoice"><br/> 

     <input type="hidden" name="code<?php echo $row['id']; ?>" id="code<?php echo $row['id']; 
     
    ?>" class="code" value="<?php echo $row['code']; ?>"/> 

     <input type="hidden" name="item<?php echo $row['id']; ?>" id="item<?php echo $row['id']; 
     
    ?>" class="item" value="<?php echo $row['item']; ?>"/> 

     <label for='color'>Color:</label> 

     <?php echo $row['color']; ?><br/><br/> 

     <label for='size'>Size:</label> 

     <?php echo $row['size']; ?><br/> 

     <input type="hidden" name="price<?php echo $row['id']; ?>" id="price<?php echo $row['id']; 
     
    ?>" class="price" value="<?php echo $row['price']; ?>"/> 

     <br/> 

     <label for="quantity">Quantity:</label> 

     <?php echo $row['quantity']; ?><br/><br/> 

     <input type="hidden" name="vendor<?php echo $row['id']; ?>" id="vendor<?php echo $row['id']; 
     
    ?>" value="<?php echo $row['vendor']; ?>"/> 

     <input type="hidden" name="ref<?php echo $row['id']; ?>" id="ref<?php echo $row['id']; ?>" value="
     <?php echo $row['href']; ?>"/> 

     <p class="backforward"> 
      <span class="row"><?php echo $row['id']; ?></span> 
      &nbsp;of&nbsp 
      <span class="rowsfound"><?php echo $rowsfound?>&nbsp;items</span> 
     </p> 

     <p class="bought"></p> 
      <div id="buttoncontainer<?php echo $row['id']; ?>" class="buttoncontainer"> 
      <span id="value<?php echo $row['id']; ?>" class="value" ><?php echo $row['price']; ?></span> 
      <button name="buynow<?php echo $row['id']; ?>" id="buynow<?php echo $row['id']; 
      
    ?>" class="button" onclick="addtoinvoice()" type="button" title="Buy Now!"/>Buy Now!</button>
     
    </div> 
    </fieldset> 
    </div> 
    </div> 
    </div> 

    <?php
    // end while loop
    mysql_close($link); 
    ?>  


    <link id="dognuts" rel="stylesheet" type="text/css" href="styles/product.css"/> 
    //not sure CSS link is right?
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  18. #18
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes, Wow swami, I figured it had to do with the while loop closure, I'll check it out. The other thing is the CSS link, should that be outside the loop? These are basic PHP principles I know and unfortunately I've completely missed them in any of the online demonstrations of PHP?

    while($row = mysql_fetch_array($result))
    { // start while loop
    ?>
    Also the closing '?>' after the opening while brace? Weird science.

    Actually, I'm getting the same result, most of the CSS is screwed up and getElementsBy anything are totally shot in the JS?
    Last edited by Heinz Stapff; May 6, 2012 at 09:04. Reason: Also the closing '?>' after the opening while brace? Wierd science.

  19. #19
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes, I realize now why the JS and CSS are screwed up. Before I was echoing the div '#rowcontainer' for every div '#rowcell' which added an extra element for every row found in the database, thereby screwing up any nodes referenced etc.

    This means a total overhaul of the JS.

    As long as this is the correct PHP construct, I'll proceed with changes. ie. 'The CSS link is outside of the while loop?' and 'The opening and closing ?> PHP tags are in the right place as you have demonstrated?'

    Thanks again for you're patience.

  20. #20
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes, everything is working fine now, including the CSS and JS, there where no problems with the node values after all!? Now I've changed the PHP construct to the correct methode I'm still back to the original post problem.
    Conditional echo on update field?
    if and else only return the first statement?

    Button snipit
    PHP Code:
    <button 
    name="buynow<?php echo $row['id'];?>
    id="buynow<?php echo $row['id'];?>
    class="button" onclick="addtoinvoice()" type="button" 
    title="
    <?php if($row['sold']=='Sold!')
    echo 
    $row['sold'];//returns
    else
    echo 
    'Buy Now';//returns empty?
    ?>
    "/>
    <?php 
    if($row['sold']!==' ')
    echo 
    $row['sold'];//returns 'Sold!'
    else
    echo  
    'Buy Now';//returns empty button with no innerText???:nono:
    ?>
    </button>

  21. #21
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes, I changed the if else around but it still only returns the 'Sold!' statement and the other buttons have blank titles and no innerText?

    Button Snipit
    PHP Code:
    <button 
    name="buynow<?php echo $row['id'];?>
    id="buynow<?php echo $row['id'];?>
    class="button" onclick="addtoinvoice()" type="button" 
    title="
    <?php 
    if($row['sold']==' ')
    echo 
    'Buy Now!';//returns blank title??
    elseif($row['sold']!==' ')
    echo 
    $row['sold'];//returns 'Sold!'
    ?>
    "/>
    <?php 
    if($row['sold']==' ')
    echo 
    'Buy Now!';//returns buttons with no innerText??
    elseif($row['sold']!==' ')
    echo 
    $row['sold'];//returns buttons with innerText 'Sold!' ??
    ?>
    </button>
    I need both buttons returned

    Button Snipit 2
    PHP Code:
    <button 
    name="buynow<?php echo $row['id'];?>
    id="buynow<?php echo $row['id'];?>
    class="button" onclick="addtoinvoice()" type="button" 
    title="
    <?php 
    if($row['sold']!==' ')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    "/>
    <?php 
    if($row['sold']!==' ')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    </button>
    Produces same result???!!!!!!!!!
    Last edited by Heinz Stapff; May 6, 2012 at 11:23. Reason: Button Snipit 2

  22. #22
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Conditional echo on update field?

    Mandes, finaly got the right reaction though I can't understand why the title appears like I wrote a '\n' newline and the title is on the second line with a blank line above? Weird

    Button Snipit that works?
    PHP Code:
    <button 
    name="buynow<?php echo $row['id'];?>
    id="buynow<?php echo $row['id'];?>
    class="button" onclick="addtoinvoice()" type="button" 
    title="
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];//returns on second line of title?
    else
    echo 
    'Buy Now!';//returns on second line of title?
    ?>
    "/>
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];//returns good?
    else
    echo 
    'Buy Now!';//returns good?
    ?>
    </button>
    Hey, I noticed I'm not having the same responce with the onclick of the button? I'd probably have to change the className to avoid the JS responce. This is what I tried with the button onclick that doesn't seem to work even though I added the 'bought()' function to the JS.

    Onclick Button Snipit
    PHP Code:
    <button 
    name="buynow<?php echo $row['id'];?>
    id="buynow<?php echo $row['id'];?>
    class="button" 
    onclick="//onclick
    <?php 
    if($row['sold']=='Sold!')
    echo 
    'addtoinvoice()';
    else
    echo 
    'bought()';
    ?>

    type="button" 
    title="
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    "/>
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    </button>
    Function bought
    Code:
    function bought()
    {
    alert('Congratulations! You purchased an original\nThere are no more of this item.');
    }

  23. #23
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Conditional echo on update field?

    Mandes, sorry it does work on the onclick, I just got it backwards with the function call, it actually should be like this:

    Onclick Button Snipit
    PHP Code:
    <button 
    name="buynow<?php echo $row['id'];?>
    id="buynow<?php echo $row['id'];?>
    class="button" 
    onclick="
    <?php 
    if($row['sold']=='Sold!')
    echo 
    'bought()';//not addtoinvoice()
    else
    echo 
    'addtoinvoice()';//not bought()
    ?>

    type="button" 
    title="
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    "/>
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    </button>
    Still would like to know what can fix the title output and stop it acting like a new line was written in the code? Other than that I'm more than grateful for your instruction. I could have read every post on the web and still not have known the proper PHP construct syntax for a simple GET request. Muchos mas gracias a te.

  24. #24
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Conditional echo on update field?

    Acctually had to change the className of the button so the complete PHP code is as follows, thanks again for youre instruction Mandes

    Button Snipet
    PHP Code:
    <button 
    name="buynow<?php echo $row['id'];?>
    id="buynow<?php echo $row['id'];?>
    class="
    <?php 
    if($row['sold']=='Sold!')
    echo 
    'soldbutton';//prevents generic button onclick action
    else
    echo 
    'button';
    ?>

    onclick="
    <?php 
    if($row['sold']=='Sold!')
    echo 
    'bought()';
    else
    echo 
    'addtoinvoice()';
    ?>

    type="button" 
    title="
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    "/>
    <?php 
    if($row['sold']=='Sold!')
    echo 
    $row['sold'];
    else
    echo 
    'Buy Now!';
    ?>
    </button>
    Still have titles showing up on second line though, anbody got a clue how to stop that?


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
  •