mweingand Posted September 1, 2014 Share Posted September 1, 2014 I'm new to PHP and I have an html page I would like to pull specific data into certain areas on the page that I will modified for php.Here are some details. The page is a static html page that has the prices of 40+ products in a standard <li> list. When I update the prices in our database through our shopping cart, I have to change these prices manually in the html. https://www.novon.com/dynamic_mixers.html. It would be great if I could link the field for Base_Price in the products table to each affiliated <li> tag,I am pulling all the data I need into ($results) and can display it all but I don't know how to get just the single products Base_Price in my <li> tag. <?=$row['Base_Price'] ?>I know this code is not correct but to get the point across, can I create a variable that I define in each <li> tag with a statement like,,,, <?=$row['Base_Price with Product_Code=123456'] ?>I could create a new SQL query for each <li> like "SELECT Base_Price from Products WHERE Product_Code='123456' ", but that's a lot of calls to the DB and a lot of code on the page.And just so I weed out the hard core coders, I can not rebuild the entire page. This is too big of a project for me and my limited coding with PHP.Can anyone help??Thanks in advanceMichael Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 2, 2014 Share Posted September 2, 2014 Pull all of your relevant data from the database in one query. I suppose you are using an auto_incremented ID, or some other UNIQUE key. You can store all the data in an array, and then echo out that array as the base price. If you cannot re-build the whole page to make it dynamic, this is my suggestion. Example <?php $link = mysqli_connect("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $type = "mixers"; /* create a prepared statement */ if ($stmt = mysqli_prepare($link, "SELECT Product_Code, Base_Price FROM Product WHERE Type=?")) { /* bind parameters for markers */ mysqli_stmt_bind_param($stmt, "s", $type); /* execute query */ mysqli_stmt_execute($stmt); /* bind result variables */ mysqli_stmt_bind_result($stmt, $product, $price); /* fetch value */ while(mysqli_stmt_fetch($stmt)) { $products[$product] = $price; } /* close statement */ mysqli_stmt_close($stmt); } /* close connection */ mysqli_close($link); ?> After this, anywhere you need the price you can echo out the array. <h2 align="center" class="topper"><a href="dynamic_mixer_smx800e.html">SMX 800E <br /> Mixer</a></h2> <ul> <li>Length 27" Tube</li> <li>800 Watts </li> <li><?php echo $product['smx800e']; ?></li> </ul> Quote Link to comment Share on other sites More sharing options...
mweingand Posted September 2, 2014 Author Share Posted September 2, 2014 First off, Thanks Guru. I really thought I totally understood this but I am not even getting an error anymore to point me in the right direction. Can you see where I may have gone wrong? I changed the query to use record_number as I do not have all Product_Code 's for all the units but for now I have good record_number 's. I then figured that if I put the record_number in the echo to call the variable in, I'd see the Base_Price. <?php echo $product['1072']; ?> Im also not sure what Type=? is doing in my query. When I change the query as follows, (SELECT record_number, Base_Price FROM products WHERE Unit_Product='u') I get an 's' in my output field. At least it's pulling something, just not sure what. Here is my code with user name and password omitted. Any help is greatly appriciated. ******************************************************************************* <div align="center">PHP coding for price only in 'li' tag.</div><div> <?php$link = mysqli_connect("localhost", "users_name", "pass", "db");/* check connection */if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit();}$type = "mixers";/* create a prepared statement */if ($stmt = mysqli_prepare($link, "SELECT record_number, Base_Price FROM products WHERE Type=?")) { /* bind parameters for markers */ mysqli_stmt_bind_param($stmt, "s", $type); /* execute query */ mysqli_stmt_execute($stmt); /* bind result variables */ mysqli_stmt_bind_result($stmt, $product, $price); /* fetch value */ while(mysqli_stmt_fetch($stmt)) { $products[$product] = $price; } /* close statement */ mysqli_stmt_close($stmt);}/* close connection */mysqli_close($link);?><h2 align="center" class="topper"><a href="dynamic_mixer_smx800e.html">SMX 800E <br />Mixer</a></h2><ul><li>Length 27" Tube</li><li>800 Watts </li><li><?php echo $product['1072']; ?></li></ul></div> Quote Link to comment Share on other sites More sharing options...
mweingand Posted September 2, 2014 Author Share Posted September 2, 2014 !!!!HELP Anyone!!!!!!!! Please OK here is some questions after some testing. I'll put what I've done, results, Questions and then my code.What I've done.After no sucess.I took out TYPE=? in the query and made the query like this. "SELECT Product_Code, Base_Price FROM products WHERE Unit_Product='u'" I got an s in my output. I found that this was coming from the first letter "only" in the Product_Code field of the first record it found matching the query. I tried changing the field to verify it was coming from that field. It was, but again only the "first charactor". I then changed the output line to what I thought it would be, using as the Constant, "SMX800E" (coming from Product_Code but this had no effect. I also noticed that we were not grabing $price so I changed that to. <li><?php echo $price['SMX800E']; ?></li> I am getting only the first charactor of the Base_Price field. I changed the amount to verify. I then took out SMX800E from the variable and put in a numeric '1' and it pulled the second charactor only from the field so I tested by changing the number to a 2,3,4 and so on. It's just grabing one charactor from the field in that place holder. 0,1,2,3,4 place. So the two issues Im having. Im not able to pull using a variable and I am only getting one charactor in my output. here is my code. ********************* $type = "mixers";/* create a prepared statement */if ($stmt = mysqli_prepare($link, "SELECT Product_Code, Base_Price FROM products WHERE Unit_Product='u'")) { /* bind parameters for markers */ mysqli_stmt_bind_param($stmt, "s", $type); /* execute query */ mysqli_stmt_execute($stmt); /* bind result variables */ mysqli_stmt_bind_result($stmt, $product, $price); /* fetch value */ while(mysqli_stmt_fetch($stmt)) { $products[$product] = $price; } /* close statement */ mysqli_stmt_close($stmt);}/* close connection */mysqli_close($link);?><h2 align="center" class="topper"><a href="dynamic_mixer_smx800e.html">SMX 800E <br />Mixer</a></h2><ul><li>Length 27" Tube</li><li>800 Watts </li><li><?php echo $price['SMX800E']; ?></li></ul> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 3, 2014 Share Posted September 3, 2014 I can not rebuild the entire page. but, to get the prices from a database table and list them for all the products on the page, YOU WILL BE CHANGING THE ENTIRE PAGE. you might as well dynamically produce the page and save the time it would take you to add php code in 40+ places on the page. Quote Link to comment Share on other sites More sharing options...
mweingand Posted September 3, 2014 Author Share Posted September 3, 2014 Pull all of your relevant data from the database in one query. I suppose you are using an auto_incremented ID, or some other UNIQUE key. You can store all the data in an array, and then echo out that array as the base price. If you cannot re-build the whole page to make it dynamic, this is my suggestion. Example <?php $link = mysqli_connect("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $type = "mixers"; /* create a prepared statement */ if ($stmt = mysqli_prepare($link, "SELECT Product_Code, Base_Price FROM Product WHERE Type=?")) { /* bind parameters for markers */ mysqli_stmt_bind_param($stmt, "s", $type); /* execute query */ mysqli_stmt_execute($stmt); /* bind result variables */ mysqli_stmt_bind_result($stmt, $product, $price); /* fetch value */ while(mysqli_stmt_fetch($stmt)) { $products[$product] = $price; } /* close statement */ mysqli_stmt_close($stmt); } /* close connection */ mysqli_close($link); ?> After this, anywhere you need the price you can echo out the array. <h2 align="center" class="topper"><a href="dynamic_mixer_smx800e.html">SMX 800E <br /> Mixer</a></h2> <ul> <li>Length 27" Tube</li> <li>800 Watts </li> <li><?php echo $product['smx800e']; ?></li> </ul> First off thanksjcbones for gettting me close. NOW !!!HELP Anyone!!!!!!!! Please OK here is some questions after some testing. I'll put what I've done, results, Questions and then my code. What I've done. After no sucess. I took out TYPE=? in the query and made the query like this. "SELECT Product_Code, Base_Price FROM products WHERE Unit_Product='u'" I got an s in my output. I found that this was coming from the first letter "only" in the Product_Code field of the first record it found matching the query. I tried changing the field to verify it was coming from that field. It was, but again only the "first charactor". I then changed the output line to what I thought it would be, using as the Constant, "SMX800E" (coming from Product_Code but this had no effect. I also noticed that we were not grabing $price so I changed that to. <li><?php echo $price['SMX800E']; ?></li> I am getting only the first charactor of the Base_Price field. I changed the amount to verify. I then took out SMX800E from the variable and put in a numeric '1' and it pulled the second charactor only from the field so I tested by changing the number to a 2,3,4 and so on. It's just grabing one charactor from the field in that place holder. 0,1,2,3,4 place. So the two issues Im having. Im not able to pull using a variable and I am only getting one charactor in my output. here is my code. ********************* $type = "mixers"; /* create a prepared statement */ if ($stmt = mysqli_prepare($link, "SELECT Product_Code, Base_Price FROM products WHERE Unit_Product='u'")) { /* bind parameters for markers */ mysqli_stmt_bind_param($stmt, "s", $type); /* execute query */ mysqli_stmt_execute($stmt); /* bind result variables */ mysqli_stmt_bind_result($stmt, $product, $price); /* fetch value */ while(mysqli_stmt_fetch($stmt)) { $products[$product] = $price; } /* close statement */ mysqli_stmt_close($stmt); } /* close connection */ mysqli_close($link); ?> <h2 align="center" class="topper"><a href="dynamic_mixer_smx800e.html">SMX 800E <br /> Mixer</a></h2> <ul> <li>Length 27" Tube</li> <li>800 Watts </li> <li><?php echo $price['SMX800E']; ?></li> </ul> Quote Link to comment Share on other sites More sharing options...
mweingand Posted September 3, 2014 Author Share Posted September 3, 2014 Please be gentile I am new to PHP and I am struggling with what I thought would be an easy, normal, standard call to the db to get a variable based on a constant, to place in an 'li' tag. jcbones was helpfull in getting me the code below. When I put this in place there are a few strange things that happen. 1. The output it only the first charactor of the field, that I am trying to pull. Base_Price. example: the first charactor in 1670.00 is a 1. 1 shows in the output only. 2. The query looks to be tryign to call a field that I do not have. TYPE=? So I changed this to be WHERE Unit_Product='u'. I edited for three of my product to have a 'u' in this field. It pulls fine manually. ******(Info from jcbones)******* /*Pull all of your relevant data from the database in one query. I suppose you are using an auto_incremented ID, or some other UNIQUE key. You can store all the data in an array, and then echo out that array as the base price. */ /*If you cannot re-build the whole page to make it dynamic, this is my suggestion. */<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit();}$type = "mixers";/* create a prepared statement */if ($stmt = mysqli_prepare($link, "SELECT record_number, Base_Price FROM Product WHERE Type=?")) { /* bind parameters for markers */ mysqli_stmt_bind_param($stmt, "s", $type); /* execute query */ mysqli_stmt_execute($stmt); /* bind result variables */ mysqli_stmt_bind_result($stmt, $product, $price); /* fetch value */ while(mysqli_stmt_fetch($stmt)) { $products[$product] = $price; } /* close statement */ mysqli_stmt_close($stmt);}/* close connection */mysqli_close($link);?>/* After this, anywhere you need the price you can echo out the array. */ <h2 align="center" class="topper"><a href="dynamic_mixer_smx800e.html">SMX 800E <br />Mixer</a></h2><ul><li>Length 27" Tube</li><li>800 Watts </li><li><?php echo $product[‘1077”]; ?></li></ul> Quote Link to comment Share on other sites More sharing options...
mweingand Posted September 3, 2014 Author Share Posted September 3, 2014 but, to get the prices from a database table and list them for all the products on the page, YOU WILL BE CHANGING THE ENTIRE PAGE. you might as well dynamically produce the page and save the time it would take you to add php code in 40+ places on the page. unfortunately I don't have the knowlage on how to create a dynamically produced page, so Im looking for help at the level I'm at now. If you have suggestions of what I've done wrong in this thread, I'd aprriciate it. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 3, 2014 Share Posted September 3, 2014 the array that the database retrieval code is storing the results into is $products. if 1077 is the index value that corresponds to your SMX 800E example, you would use echo $products['1077']; this is why copy/pasting code is not learning. the code that gets posted is only an example to look at, and in just about every case is untested, and can contain syntax errors, hastily typed variable names, logical errors... 1 Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 4, 2014 Share Posted September 4, 2014 On top of what mac_guyver pointed out, in post #6 you build the $products array then attempt to print $price['SMX800E']. $price is a string, $products is an array. If I'm not mistaken, php will attempt to return the character at the specified index of the string if you attempt to use the string like an array. I'm not absolutely certain if this last point is true - it may just throw an error. And, admittedly, I'm not going to be chuffed to find out because it's just wrong, so I'd recommend checking your variables and avoiding the issue entirely. Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 4, 2014 Share Posted September 4, 2014 Mac_Gyver, and Maxxd got it. Both $product and $price are strings. So you need to use $products. This is set in the while loop, if you look carefully. I would also echo Mac_Gyver's post above. It would be easier to just dynamically build the page than have to change 40+ products per page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.