Jump to content

Barand

Moderators
  • Posts

    24,604
  • Joined

  • Last visited

  • Days Won

    830

Everything posted by Barand

  1. Something like this? <?php $data = " <Message> <EntityType>Document</EntityType> <ProductSETS> <Entity> <id>1</id> <Specification1>Name</Specification1> <Specification2>Color</Specification2> <Specification3>Type</Specification3> <Specification4>Manufacturer</Specification4> <Price>Product Price</Price> </Entity> <Entity> <id>100</id> <Specification1>Car model 1</Specification1> <Specification2>Red</Specification2> <Specification3>Hybrid car</Specification3> <Specification4>Telsa</Specification4> <Price>10000</Price> </Entity> <Entity> <id>101</id> <Specification1>Car model 2</Specification1> <Specification2>Blue</Specification2> <Specification3>Diesel car</Specification3> <Specification4>Ford</Specification4> <Price>5000</Price> </Entity> <Entity> <id>102</id> <Specification1>Car model 3</Specification1> <Specification2>Black</Specification2> <Specification3>SUV</Specification3> <Specification4>Hummer</Specification4> <Price>8000</Price> </Entity> </ProductSETS> </Message> "; $feed = simplexml_load_string($data); $out = ''; foreach ($feed->ProductSETS->Entity as $prod) { $out .= "<div class='product'>\n"; foreach ($prod as $k => $spec) { if ($k != 'id') $out .= "$spec<br>"; } $out .= "</div>\n"; } ?> <!DOCTYPE html> <html lang='en'> <head> <title>Example</title> <meta charset='utf-8'> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#find").click( function() { var tid = $("#tid").val() $.get( "", // specify processing file on server (in this case it's same file) {"ajax":"teacher", "tid":tid}, // data to send in request function(resp) { // handle the reponse $.each(resp, function(k,v) { $("#"+k).val(v) }) }, "JSON" // response type ) }) }) </script> <style type='text/css'> body { font-family: calibri, sans-serif; font-size: 12pt; } div.product { margin: 16px; padding: 8px; width: 200px; float: left; } </style> </head> <body> <?= $out ?> </body> </html>
  2. The basics of AJAX are simple. You send a request to be processed on the server and it sends back a response. When responding to an AJAX request, anything output by the server program (stuff that would normally go to the browser screen) is sent back as the response. Here's a simple example (using data from the DB in my tutorial) which accepts a teacher_id, sends it in a request, and recieves back the data for that teacher thne displays it. <?php require 'db_inc.php'; $db = pdoConnect('jointute'); // my cuton connection function - use your connection code //******************** PROCESS AJAX REQUESTS ********************* // // if (isset($_GET['ajax'])) { if ($_GET['ajax']=='teacher') { exit(json_encode(getTeacher($db, $_GET['tid']))); } else { exit('INVALID'); } } // // Functions // function getTeacher($db, $id) { $res = $db->prepare("SELECT t.fname , t.lname , d.deptname as dept FROM teacher t JOIN department d USING (dept_id) WHERE teacherid = ? "); $res->execute([ $_GET['tid'] ]); $row = $res->fetch(); if ($row) { return $row; } else return ['fname'=>'NOT FOUND', 'lname'=>'', 'dept'=>''] ; } ?> <!DOCTYPE html> <html lang='en'> <head> <title>Example</title> <meta charset='utf-8'> <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#find").click( function() { var tid = $("#tid").val() $.get( "", // specify processing file on server (in this case it's same file) {"ajax":"teacher", "tid":tid}, // data to send in request function(resp) { // handle the response $.each(resp, function(k,v) { $("#"+k).val(v) }) }, "JSON" // response type ) }) }) </script> <style type='text/css'> body { font-family: calibri, sans-serif; font-size: 12pt; } div { margin: 16px; padding: 8px; border: 1px solid gray; } label { display: inline-block; background-color: black; color: white; width: 120px; padding: 8px; margin: 1px 8px;; } </style> </head> <body> <div> <label>Teacher ID (1-20)</label> <input type='number' id='tid' value='0'> <button id='find'>Find</button> </div> <div> <label>First name</label><input type='text' id='fname' value='' readonly><br> <label>Last name</label><input type='text' id='lname' value='' readonly><br> <label>Department</label><input type='text' id='dept' value='' readonly> </div> </body> </html>
  3. Barand

    Join error

    Are you sure you want that LEFT JOIN and not a normal JOIN? There will always be a match with mywines_activity records. LEFT JOINS are slow.
  4. Barand

    Join error

    Well, the error disappeared when I did it that way. You can only do INNER JOINS with the FROM A, B, C method. You have to put the join conditions in the WHERE clause which mixes the table structure with selection criteria In my experience, explicit joins are faster.
  5. Barand

    Join error

    Sorry it's taking a while. I had to create the tables to check the query. If i structure the FROM clause correctly, using explicit JOINS with ON clauses, the error disappears FROM mywines_activity a JOIN mywines_activity_codes d ON a.activity = d.activity_code JOIN mywines w USING (wine_num) LEFT OUTER JOIN (SELECT wine_num, max(date_posted) as sort_date FROM mywines_activity GROUP BY wine_num) s ON s.wine_num = a.wine_num
  6. Barand

    Join error

    You posted in "PHP Coding Help". I moved it to MySQL forum
  7. Barand

    Join error

    What sequence do you want? It usually gives the line number within the query when an SQL error is reported - what is it?
  8. In my experience, between is usually inclusive EG mysql> select id, prod_name -> from products -> where id between 2 and 4; +----+-----------+ | id | prod_name | +----+-----------+ | 2 | Bbb | | 3 | Ccc | | 4 | Ddd | +----+-----------+ and the example given by the OP certainly implies that is the case here too.
  9. Having declared that no sorting is required in any part, your solution depends on the array being sorted. eg echo func([20, 10, 15, 30, 40], 20, 30); // 50 instead of 75
  10. What have you tried so far, and which bit is giving you a problem? You may find useful... array_search()
  11. Now it returns 10 again function ec($n, array $a, array $b) { return $n * $b[1] - $b[0] * $a[0] * $a[1] ; } echo ec( 4, [1,3], [2,4]); // 10 If you post this topic again you will be banned.
  12. You would have to remove "school" from the query. DISTINCT applies to the whole row. What you are doing also has other problems. You could, say, have two different Bill Smith's signed up. One could even have signed up a second time as William Smith.
  13. In the 1980's there was a PC program generation application called "The Last One" which was marketed as "No one will ever need to write a program again". Needless to say, it failed miserably. I get the impression you are trying to create another "Last One". No one will ever need to write another query again. Seriously!?
  14. In my (humble) opinion. I agree wholeheartedly with the above opinions on PDO
  15. Here you are. Now it returns 10 function ec($n, array $a, array $b){ return 10; } We are not going to do your assignment for you.
  16. What substr? How does that image show your current code? I gave it a second try... enough is enough. Good luck.
  17. Why the two queries when the first set of results is just a subset of the second?
  18. I haven't a clue. Imagine I am not psychic and cannot see your screen or your current code.
  19. It's one way. IMO< Your original use of <a>...</a> links is probably more common. You can always style the links to resemble buttons. Also, as all you are doing is GETting a new page of data, the convention is to use GET and not POST method. Use POST when the form action causes change - such as logging in or updating a database.
  20. You old query stored its results in $sorgu. Your new paginated query stores its results in $result. You need to adjust the code accordingly.
  21. When it comes to SQL injection, stored procedures and prepared statements both do the same thing - they separate the data values fron the query statement. The mechanisms are slightly different, however. With prepared statements, placeholders are used instead of the data, and the values are passed at execution time. With stored procedured, the values are passed as input parameters when the procedure is called. Using both is a "belt and braces (sorry, suspenders)" approach. The advantage of a sproc is it is pre-written and stored (clue is in the name) waiting to be called so there is no overhead of sending the query statement - you just send the data. If you use "dynamic sprocs" you throw away this advantage. To me the idea of a "dynamic sproc" is oxymoronic, like military intelligence, internet privacy, common sense.
  22. All I'm seeing in your posts is... This is what I'm doing Can you help?
  23. I was in my fifties when I first came across something called HTML. I knew Basic from from my days with a BBC home microcomputer so I started using Visual Basic to create web pages. Daily, I would log in to the Compuserve Bulletin Board using my dial-up modem to exchange ideas (much like now - plus ca change, plus ca meme). I started to write Java applets to enhance my pages but, no sooner had I started to become reasonably proficient, the world switched to Flash (which has since died a death). Disheartened, I let the Java lapse. One of the biggest mistakes I've made as today's phone apps are essentially Java applets. A year or so later I came across PHP and much prefered its Java/C type syntax to Basic. My preference was cemented when I discovered that rewriting my VB/ASP scripts in PHP gave me a 3x+ speed increase (in one case a 70x increase in performance as VB was crap at handling long string concatenations). Anyway, the moral is "You ain't too old".
  24. Also, don't run a series of chained queries where you use the result of one query to determine the records selected in the next. Instead use a single query with JOINs SELECT um.id , um.username , um.lastname , bs.slot_id , bs.slot_date FROM usermanagement um JOIN booking_reservation br ON um.username = br.reservation_name JOIN booking_slots bs ON br.slot_id = bs.slot_id ORDER BY um.username, bs.slot_date
  25. Yeah, but he needed to have it pointed out in a picture where he went wrong. I can do that now he finally deigned to show the output code.
×
×
  • 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.