Jump to content

Steveinid

Members
  • Posts

    14
  • Joined

  • Last visited

Steveinid's Achievements

Member

Member (2/5)

0

Reputation

1

Community Answers

  1. When you initially arrive to the page there is nothing set so it tells the visitor to 'Enter a valid age'.
  2. try this code: $myVar = htmlspecialchars($_GET["example"]); $sql = 'SELECT COUNT(id) AS total FROM myTable WHERE myField = :myvar;'; $arguments = ['myvar' => $myVar]; if (!$arguments) { return $conn->query($sql); } $statement = $conn->prepare($sql); $statement->execute($arguments); $row = $statement->fetch(); $total = $row['total']; if ($total > 0) { echo 'That is already stored.'; } else { // INSERT $myVar into the database }
  3. I have a web page where users can scroll down to see the content. Along the length of the page are links to another page. When the user clicks a link and gets to the next page they are presented a link to 'return to the previous page'. If they click that link it takes them back to the original page just fine but it returns them to the top of the page. I would like them to return to the same spot they were at when they exited the original page. When you use the 'back' button on the browser it does this. I don't know what the term is for it so I don't even know where to start looking for a way to do it. Any help would be appreciated. Thanks Steve
  4. Give this a try for your code: <?php if (!isset($_POST['age'])) { echo 'enter a valid age'; } else { $age = $_POST['age']; if($age >=18) { echo "you can vote!!!"; } else { echo "You are not allowed to vote"; } } ?>
  5. I figured this might be a simple answer... Didn't think of the 'OR' operator. I look at this and my initial question is how does it know which is which? However, after staring at it for too long am I right to guess (me being rather new at this) that it searches both columns and outputs based on the results? Since no city will ever match a zip code and vice versa it has no choice but to choose one. Just trying to understand fully what I'm doing. Thank you for the answer. I can now go back and fix my code that I messed up yesterday.
  6. I have a database with a bunch of businesses. Each business has an address with zip code. The user will want to find businesses based on location such as city or zip. I can set up a form that will find by city and a separate form that will find by zip. But, I would like to have a form that does all in one. A point in the right direction would be helpful. Thank you.
  7. I have a database with prices. I am displaying all prices from lowest to highest. Not all products in the database are updated with prices so by default they show $0.00 and updated products show their actual price like $5.49 for example. I have set it so that it will show the price as 'Not Updated' if the price is actually 0.00. (See code below) However numerically it is still 0.00 as far as the 'ORDER by' command is concerned. if ($row["current_price"] === 0.00 ) { echo "<span style='font-size:85%;'>" . $row['product_name'] . "</span>" . " - " . "<span style='color: red'>" . "<span style='font-size:70%;'>Not Updated</span>" . "</span><br>"; I want to order it so that all products with prices that = 0.00 are posted after products with actual prices but still showing the lowest price first so it would look as follows: product 1 = $2.99 product 2 = $3.29 product 3 = Not Updated My work around is to actually make all prices by default a very high price of 50,000 for example (since no price in the database will be that high for any of the products) then I change the above code to : if ($row["current_price"] > 49999.99) { echo "<span style='font-size:85%;'>" . $row['product_name'] . "</span>" . " - " . "<span style='color: red'>" . "<span style='font-size:70%;'>Not Updated</span>" . "</span><br>"; ...and this works fine as far as a visitor is concerned but I don't like that I've had to do that. Thanks for the help.
  8. Thank you for the quick reply. I tried things like echo $arr[0] or echo $arr[store_name] etc... but nothing ever worked. I spent most of Sunday working on this. It's a good thing programming is not like looking at the sun. I'd be blind by now I used the print_r() successfully and was able to view the array while I was working on it. It made things much clearer. I do have the first column as the ID and it is the "autoincrement primary index". My lookup table is dependent on the ID of the store table and the product table so they have to be unique. I did SELECT the ID column first as you suggested. It was a key factor in making this work. I originally had the store name selected first. FINALLY, because of your last post all is working well. I am able to display the additional information along with the store name. You have helped me more than you know. When I'm done with this project I'll send you a link so you can see what you did. It is greatly appreciated. Thank you Steve
  9. I spent quite a bit of time figuring this out but I finally got the results I was asking for. It successfully outputs the name of the store followed by their respective products. I had to use the current() function I couldn't figure out how to "reference elements in $arr[0] to display the unique store information" so I ended up using the current() function which worked perfectly. Here is the code I am using: $result_data = $stmt->fetchAll(PDO::FETCH_GROUP); if(!$result_data) { echo 'There is no data to display.'; } else { foreach($result_data as $arr) { echo "<B>" ; echo current($arr[0]); echo "</B>"; echo "<br>"; foreach($arr as $row) { echo $row['product_name'] . " - " . $row["current_price"] . "<br>" . "<br><br>"; } end($result_data); } } So, now, I have run into another problem and I don't think the above code is capable of solving it. I mentioned that I wanted the store name to display followed by its products. That is exactly what I got. WELL, since some of the stores have the same name, I need to add other unique information with the store name like the address. (I didn't think about this when I requested help) I tried to add the additional information using what I thought was the obvious solution... inserting echo current($arr[1]) which holds the address information after echo current($arr[0]) which is the store name. It didn't work. I don't think FETCH_GROUP is the end solution to my goal. Would a subquery in the SELECT statement be the solution I'm looking for? Thanks again for the great help. It's appreciated. Steve
  10. @mac_gyver Thanks again for the help. I think this is exactly what I'm looking for. I'm a beginner and I'm a little slow understanding a lot of this. I'll let you know how it turns out. @phppup No worries. I'm not one for handouts. There are plenty of people who don't want to put in the time so I understand. I hope to get to the point where I can start helping others myself. It's a long way out but It may happen. We'll see. I just got a new book called "PHP & MYSQL" by Jon Duckett. It looks very promising for a beginner like me. It explains in detail how all of this stuff works. Unfortunately it doesn't have FETCH_GROUP in it.
  11. I imagined something similar using the FOREACH loop but wasn't sure how to go about it. I'm new at this and am struggling. Thank you, your reply is a great help. Steve
  12. I have a database with stores and products. The stores are in one table and the products are in another. I have them 'joined' in a lookup table. Now I want to display the stores and their associated products. The output would look like this: Store1 product 1 product 2 product 3 Store 2 product 1 product 2 product 3 Not all stores have the same products. They are separated by category and sub category. Users would display the products using a drop down list or some other method of choosing the list of products. I have been able to display each product individually so the output is like this: Store 1 product 1 Store 1 product 2 etc.... Here is the code I'm using: I'm sure there are security issues. At the moment i'm not concerned with that. When I get it figured out then I'll address the security issues. Any help is greatly appreciated. Steve
×
×
  • 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.