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!::x
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.
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?
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
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.
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
$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.
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.
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.
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.
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:x
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!
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—
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?
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.
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?
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?’
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?