Jump to content

All Activity

This stream auto-updates     

  1. Past hour
  2. You're much more likely to get some help if you can show some initiative on your part first. Like, if you've tried writing the code yourself. Or if you have something partially done to show and don't know how to continue with it.
  3. Today
  4. hello i need a script that sends data from the searchbar to a search.php and there the entries are displayed with the category someone knows how to write it or has someone an example https://prnt.sc/ujzvuf
  5. Thank you, I fully understand that and thank you for your input, but everything I seem to do with this framework seems to give me problems. So I have ditched it, so I no longer need any more answers on the topic. Yesterday I carried out some testing with Laravel, Cake and CodeIgniter and out of the 3 I personally like CI so going to run with that. Thanks again for all your help
  6. Hi gizmola, I typically do not understand certain mysqli code and i usually ignore it. I am happy with pdo and pdo is usually suggested by experts, like you, anyway. Meantime, i should also explain my code and repost it. You may notice a break(2) and wonder why it exists, for example. My code acually contains a switch based upon a form post. I keep all of my bookmark db code in one file (bookmarksdb). I have a page to display all bookmarks (case 'select'), a page method to add a bookmark on bookmarkable pages (case 'insert') and a page to delete bookmarks from your bookmarks list (case 'delete'). I also use a counter case which simply returns the count ($result['total']). The counter (case 'count') is used as a small circle that holds the count of bookmarks to be displayed at the top right div of the bookmarks page icon. I use it like a phone apps new messages bubble beside app icons. Thus, the icon count bubble will inform you how many bookmarks you have to avoid a need to view the bookmarks page just for this info. <?php declare (strict_types = 1); $dbhost = '127.0.0.1'; $dbname = 'test'; $dbuser = 'root'; $dbpass = ''; $dbattr = array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); try { $connection = new PDO("mysql:host=$dbhost; dbname=$dbname; charset=utf8mb4", $dbuser, $dbpass, $dbattr); } catch (PDOException $e) { error_log($e->getMessage()); } $query = 'SELECT count(*) as total FROM bookmarks WHERE uid = :uid'; $stmt = $connection->prepare($query); $stmt->execute(array(':uid' => $uid)); $result = $stmt->fetch(); switch ($action) { //$action is known by session array key which holds the csrf token of the form field posted case 'delete': //code to delete a bookmark, which relies upon !empty($result['total']) break; case 'insert': if ($result['total'] < 100) { $query = 'SELECT link FROM bookmarks WHERE uid = :uid'; $stmt = $connection->prepare($query); $stmt->execute(array(':uid' => $uid)); while ($column = $stmt->fetch()) { if ($column['link'] === $link) { $duplicate = true; break(2); } } $query = 'INSERT into bookmarks (uid, type, link, icon, label, date) VALUES (:uid, :type, :link, :icon, :label, NOW())'; $stmt = $connection->prepare($query); $stmt->execute(array(':uid' => $uid, et cetera...)); } break; } I still don't know how to handle errors from the other queries. I don't want my site to break and a white page to be shown with error data. I prefer that my site remain visible and i display the error and not the software. I have no idea how to do this yet. anyway, Thank you for the info in this thread. I appreciate everyone taking time to help me. Like i said before, i am not delusional about my status here. I am not a programmer at all. I am street smart for sure and able to code despite no education in this field. I sometimes do things correctly and many times just get something to work. No matter what, i always appreciate professionals offering inside advice. I sometimes wish hackers would dump source code to big tech company websites so i can truly learn how the pros do it. I enjoy coding when i can do it correctly but i lack so much info about this industry. I often have to employ street smarts to bring a concept to life. I have no programming friends so i never receive inside intel about 'how the pros code common ideas'. ok, too much babbling and i far too much work to do today. Off i go... Best wishes.
  7. You'll have to do that. Educated guess says the image has to be a monochrome (B&W) bitmap.
  8. I will try and come back and take a look at your code, but I wanted to say that using PDO is great. I would say that the staff is universally in favor of using PDO, so no worries about learning mysqli.
  9. Yesterday
  10. Just to clarify, my response had nothing to do with MVC. It was with the Database Class itself.
  11. All you have in that array is the firstname. That's not unique. You need the first and last names. Also, don't add them as array values. When it comes time to look up a value in there with in_array(), PHP will have to scan potentially the whole array to find a match. A faster method is to use array keys; put the whole $row as the value. Well, for starters, I don't see anything trying to use $names in there. Since you're going with the approach I said before about having the first and last names be the array key, you can use isset() to tell if there is an entry according to the name parts from $item.
  12. You create a port in Windows which is mapped to the USB port - in my case named Terow. You then create your text file and copy it to the port. $file = "print.txt"; $handle = fopen($file, 'w'); $linefeed = chr(10); $Data = "This is a test\n"; $Data .= "\n"; $Data .= "Line 1"; $Data .= "\n"; $Data .= "Line 2"; $Data .= $linefeed; $Data .= $linefeed; fwrite($handle, $Data); fclose($handle); copy($file, "//localhost/terow"); If I do the same with an image, as expected it prints junk. Can't find information on how to convert the image file to data that will be accepted by the printer.
  13. Hello Barand How are you? You are the best, great option, it works like i want Best regards
  14. How does that work, exactly? Does the USB show up as a file device you can just copy files to? What does that process require you to do?
  15. I am trying to print an image to a thermal POS printer from a PHP script. I have managed to get a text file to print by saving it to a file and copying to the USB port and that works fine. Am running PHP on Windows. Would appreciate any assistance. Thanks Steve
  16. If you are starting with this (which could be the result from a table subquery) ... +----+----------------------+------------+-----------------+----------+ | id | disciplina | moduloUfcd | idcpDisciplinas | anoTurma | +----+----------------------+------------+-----------------+----------+ | 58 | Comunicação Visual | 8599 | 49 | 11 | | 59 | Comunicação Visual | 133 | 49 | 11 | | 60 | Comunicação Visual | 134 | 49 | 10 | +----+----------------------+------------+-----------------+----------+ then this query ... SELECT group_concat(id separator ', ') as ids , disciplina , group_concat(moduloUfcd separator ', ') as mods , idcpDisciplinas , anoTurma FROM gmc GROUP BY idcpDisciplinas, anoTurma; gives ... +--------+----------------------+-----------+-----------------+----------+ | ids | disciplina | mods | idcpDisciplinas | anoTurma | +--------+----------------------+-----------+-----------------+----------+ | 60 | Comunicação Visual | 134 | 49 | 10 | | 58, 59 | Comunicação Visual | 8599, 133 | 49 | 11 | +--------+----------------------+-----------+-----------------+----------+
  17. Hello gizmola and benanamen, Thank you for the informative replies. I did not know that one can "enforce uniqueness on a user/link basis". I am nescient about sql. I will have to drop my table and rebuild it now. I don't mind an extra query if there is no other way but clearly you know of a better way. I will drop the table and add the new unique constraint. Thank you for this valuable info!!! I should've expressed my desires clearly. My apologies for any vagueness in this thread. Your assumptions are correct about unique links per user. I just want to allow bookmarks but not duplicate entries for each user. I am excited to learn how to do this properly. I doff my hat to you! Thank you. I use pdo. I just started studying php two years ago in my spare time. I never learned mysqli. I made my first login script two years ago using pdo. Thus, i only know pdo. my code is slightly different than the following but only by variable names. I clean it up for posting here because some people just insist on doing things their way. such as, naming db variable $conn, $stmt etc. I use my own names. <?php declare (strict_types = 1); $dbhost = '127.0.0.1'; $dbname = 'test'; $dbuser = 'root'; $dbpass = ''; $dbattr = array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); try { $connection = new PDO("mysql:host=$dbhost; dbname=$dbname; charset=utf8mb4", $dbuser, $dbpass, $dbattr); } catch (PDOException $e) { error_log($e->getMessage()); } $query = 'SELECT count(*) as total FROM bookmarks WHERE uid = :uid'; $stmt = $connection->prepare($query); $stmt->execute(array(':uid' => $uid)); $result = $stmt->fetch(); $Total = $result['total']; if ($Total < 100) { $query = 'SELECT link FROM bookmarks WHERE uid = :uid'; $stmt = $connection->prepare($query); $stmt->execute(array(':uid' => $uid)); while ($column = $stmt->fetch()) { if ($column['bm_path'] === $bmPath) { $duplicate = true; break(2); } } $query = 'INSERT into bookmarks (uid, type, link, icon, label, date) VALUES (:uid, :type, :link, :icon, :label, NOW())'; $stmt = $connection->prepare($query); $stmt->execute(array(':uid' => $uid, et cetera...)); } I wonder if you have any suggestions about making my code better. Please remember that i can now remove the SELECT link query because you showed me how to enforce a unique link per user. Honestly, i am very happy about this info. Best wishes.
  18. So I set the query now before the foreach like that. $connection = mysqli_connect("localhost", "xx", "xx", "xx") or die("Error " . mysqli_error()); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $sql = mysqli_query($connection, "SELECT * FROM mytable"); while($row = mysqli_fetch_array($sql)) { $names[] = $row['firstname']; } Here I need some assistant. where is the mistake. Basically I have both arrays included now. foreach($data2['wow_accounts']['0']['characters'] as $key => $item) { if($row["firstname"] == $item['name'] && row["lastname"] == $item['lastname'] ) { $class_string = ''; } else { $class_string = ' class="my-additional-class"'; } echo '<tr>'; echo '<td>'; echo $item['firstname']; echo '</td>'; echo '<td>'; echo $item['lastname']; echo '</td>'; echo '<td>'; echo '<button class="btnSelect">Select</button>'; echo '</td>'; echo '</tr>'; } echo '</table>'; } }
  19. Oh great, to be honest it was to help me start to understand MVC of which I first thought it seemed quite simple until I tried to do a simple bloody count Now I am starting to wonder what ever was wrong with <? $carcount = $db->query("SELECT id FROM vehicledetails WHERE id=id"); $carscounted = $carcount->rowCount(); ?> It was a course on Udemy,(£9.99) and to be fair I thought it had given me an insight into MVC but by your response maybe not. OT but which framework would you recommend for a beginner/advancing php'er
  20. Transactions are another one. I don't really disagree with what you wrote, and in fact I linked to information that agrees with you. The problem with anything concrete is that we don't know if jodunno is using PDO or MySqli.
  21. Typically yes, that's what you see out on the net, but I would have to disagree with this method. IMO there is really only two common cases to use Try/Catch, that being the DB Connection and handling a duplicate constraint error. There is no need to litter the code base with Try/Catch blocks. (Yes, I was guilty of that until I got spanked by @Jacques1 and learned better) What "should" be done is set the PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION and let the exceptions bubble up and let PHP handle it, which it does very well. set_exception_handler can also be used if you want a custom handler for exceptions.
  22. Link please. Based on the DB class, the tutorial is less than optimal.
  23. Check what I said again. Your query on the database (1) needs to set up an array that holds all the records in it so you can look in there for a match easily, and therefore (2) it has to happen before the foreach.
  24. Something like this should work <?php foreach $rows as $row) : ?> <div class="card"> <h4 class="card-title">Card Title</h4> <p class="card-text"> User: <?php echo $row['name']; ?> <br> Date: <?php echo $row['date']; ?> </p> </div> <?php endforeach; ?> Obviously you need to style the card as you see fit
  25. Thanks for your reply I need this (58, 59 ), Comunicação Visual, ( 8599, 133) ,49, 11 60, Comunicação Visual, 134, 49, 10
  26. Actually, I think that Barand just made a small mistake, which is certainly very unusual for him The unique constraint should be: UNIQUE KEY `unq_page_link` (`user_id`, `page_link`), That will enforce uniqueness on a user/link basis, which is what I assume you are going for. This is a better solution in that you are using the db to enforce your integrity rule rather than relying on the front end code to do it for you. With that said, code around database queries needs to be written in a way that you catch and deal with database errors appropriately. Typically that is done with a try..catch block. I can't give you specifics without knowing in advance what db api you are using (mysqli vs pdo) . Here's an example out of the "(The only proper) PDO tutorial" which is highly recommended reading for anyone learning the PDO api: try { $pdo->prepare("INSERT INTO users VALUES (NULL,?,?,?,?)")->execute($data); } catch (PDOException $e) { $existingkey = "Integrity constraint violation: 1062 Duplicate entry"; if (strpos($e->getMessage(), $existingkey) !== FALSE) { // Take some action if there is a key constraint violation, i.e. duplicate name } else { throw $e; } } Assuming you are using PDO this could be adjusted to fit your problem as it is essentially the same issue you would face in dealing with the unique constraint violation to prevent a user from adding the same link multiple times. If you are using mysqli, then you should certainly read this. A general discussion of php error handling strategies is good to read. The important thing to note about this, is that you don't want to generalize error handling to try .. catch blocks around all your queries. This is a specific issue, having to do with something you expect to happen frequently. Lots of other database exceptions can happen, and you want to have a generalized exception handling solution for those.
  27. Wow thank you for your detailed repsonse, The MVC is one I followed on a tutorial online, here is a copy of my Database Lib <?php /* * PDO DATABASE CLASS * Connects Database Using PDO * Creates Prepeared Statements * Binds params to values * Returns rows and results */ class Database { private $host = DB_HOST; private $user = DB_USER; private $pass = DB_PASS; private $dbname = DB_NAME; private $dbh; private $error; private $stmt; public function __construct() { // Set DSN $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; $options = array ( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); // Create a new PDO instanace try { $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options); } // Catch any errors catch ( PDOException $e ) { $this->error = $e->getMessage(); } } // Prepare statement with query public function query($query) { $this->stmt = $this->dbh->prepare($query); } // Bind values public function bind($param, $value, $type = null) { if (is_null ($type)) { switch (true) { case is_int ($value) : $type = PDO::PARAM_INT; break; case is_bool ($value) : $type = PDO::PARAM_BOOL; break; case is_null ($value) : $type = PDO::PARAM_NULL; break; default : $type = PDO::PARAM_STR; } } $this->stmt->bindValue($param, $value, $type); } // Execute the prepared statement public function execute(){ return $this->stmt->execute(); } // Get result set as array of objects public function resultset(){ $this->execute(); return $this->stmt->fetchAll(PDO::FETCH_OBJ); } // Get single record as object public function single(){ $this->execute(); return $this->stmt->fetch(PDO::FETCH_OBJ); } // Get record row count public function rowCount(){ return $this->stmt->rowCount(); } // Returns the last inserted ID public function lastInsertId(){ return $this->dbh->lastInsertId(); } } If you could see any errors that is causing the problem it would be appreciated, I will also look at what you have previously sent and get back to you if I get stuck
  28. Yes, firstname and lastname are the only uniquie ones. I improved the query now and played it outside of the foreach loop. But still don´t work. My try: $link = mysqli_connect("localhost", "xxx", "xx", "xx"); $query = "SELECT firstname, lastname FROM mytable"; if ($result = mysqli_query($link, $query)) { while ($row = mysqli_fetch_assoc($result)) { echo '<table class="formatHTML5" id="myTable">'; foreach($data2['wow_accounts']['0']['characters'] as $key => $item) { if($row["firstname"] == $item['name'] && row["lastname"] == $item['lastname'] ) { $class_string = ''; } else { $class_string = ' class="my-additional-class"'; } echo '<tr>'; echo '<td>'; echo $item['firstname']; echo '</td>'; echo '<td>'; echo $item['lastname']; echo '</td>'; echo '<td>'; echo '<button class="btnSelect">Select</button>'; echo '</td>'; echo '</tr>'; } echo '</table>'; } }
  1. Load more activity
×
×
  • 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.