-
Posts
597 -
Joined
-
Last visited
Everything posted by Adamhumbug
-
I am brand new to PDO and i am very sure this is a simple issue but i am struggling to work it out. My connection file: <?php $host_name = '*******.hosting-data.io'; $database = '******'; $user_name = '******'; $password = '******!'; try { $pdo = new PDO("mysql:host={$host_name}; dbname={$database}", $user_name, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> and the function that i am trying to run: function getClientName($display) { $pdo = require 'includes/dbconn.php'; $sql = "SELECT id, name from clients"; $stmt = $pdo->query($sql); $clients = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($display == 'select') { if ($clients) { $out = "<div class='input-group mb-3'> <label class='input-group-text' for='inputGroupSelect01'>Client Name</label> <select class='form-select' id='inputGroupSelect01'> <option selected disabled>Please Select</option> <option value='0'>-- New Client --</option>"; foreach ($clients as $client) { $out .= "<option value='{$client['id']}'>{$client['name']}</option>"; } $out .= "</select> </div>"; } } return $out; } I am getting an error on line 9 of the function which is: $stmt = $pdo->query($sql); I am trying to figure out what is causing this error and if my PDO is not correct. I am getting the connection successful message.
-
Thanks ill take a look
-
very useful info - thanks for that - i could well be back here for help but ill have a stab.
-
I am wanting to create a pdf document. Basically a branded document that pulls data from the database and builds a quote that would be sent to a client.
-
Hi all, I wasnt sure of the best place to put this thread so would appreciate the admins advice on that. I have a web app and i am wanting it to be able to create a downloadable pdf. I am early in the process but i am assuming that i create the view that i want to pdf in html/css. Can anyone suggest the best way for me to go about creating this PDF or any third party add ons that i could be using. @Barand will be happy to know this will be a PDO project, although a simple one to start.
-
Getting no data with if($stmt -> fetch())
Adamhumbug replied to Adamhumbug's topic in PHP Coding Help
Yes, this seems to be the issue - i was only using 2 of the values - rookie. I need to look more into the error reporting for sure - thanks all for your input as always. PDO will be something that i will look into but wanted to start with it on a new project - it will come. -
I have the following function function getBallInOver($gameId){ include 'includes/dbconn.php'; $sql = "SELECT MAX(id), over_number, ball_in_over from deliveries where game_id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param('i', $gameId); $stmt->execute(); $stmt->bind_result($overno, $ballinover); if($stmt->fetch()){ $out = $overno; } $stmt -> close(); return $out; } which is called from this function function newDelivery($gameId, $runs, $fairDelivery){ include 'includes/dbconn.php'; $batterId = getOnStrikeBatter($gameId); $ballInformation = getBallInOver($gameId); $one = 1; $sql = "INSERT into deliveries (game_id, on_strike_batter_id, runs_scored, fair_delivery, over_number) values (?,?,?,?,?)"; $stmt = $conn->prepare($sql); $stmt -> bind_param('iiiii', $gameId, $batterId, $runs, $fairDelivery, $ballInformation); $stmt -> execute(); if($runs % 2 !== 0){ include 'includes/dbconn.php'; $zero = 0; $one = 1; $sql = "UPDATE batter_in set on_strike=(case when on_strike = ? then ? else ? end)"; $stmt = $conn->prepare($sql); $stmt -> bind_param('iii', $zero, $one, $zero); $stmt -> execute(); } return; } I am getting an error that the over_number cannot be null. I dont understand why the value is null - when i run the sql directly on the server i get the following: MAX(id) = 42, over_number = 1, ball_in_over = 1 Could someone point me in the right direction. For clarity, the function above its call takes the same peramaters and that works fine so i know the function is getting the data it needs.
-
returning the result of multidimentional array
Adamhumbug replied to Adamhumbug's topic in PHP Coding Help
I found what it was in my code that was outputting the extra div. I had left something in above that wasnt needed. Thanks for your help as always. -
returning the result of multidimentional array
Adamhumbug replied to Adamhumbug's topic in PHP Coding Help
I do have one little issue with this. I have the following foreach ($res as $row) { if (!isset($data[$row['house_name']])) { $data[$row['house_name']] = []; } $data[$row['name']][] = [$row['id'], $row['first_name'], $row['last_name']]; } foreach ($data as $team => $players) { $out .= "<div class='col-4'> <h5>$team</h5> "; foreach ($players as $p) { $out.= "<input class='form-control w-100 mb-3' type='text' disabled='disabled' name='' data-value='$p[0]' value='$p[1] $p[2]'>"; } $out .= "</div>"; } return $out; and i am getting a rougue div created before the 2 divs that i am expecting - you can see the 3 col-4s The first one is always blank. I am assuming that this is something simple but i am struggling to see what it is. -
Hi All, I have a php function that returns a list of players and which team they play for. I am looking to create an array that at the top level has the teams and then second level will be first and last name of the player. i think i have done this like this $stmt -> bind_result($fn, $ln, $team); $squad = []; while($stmt -> fetch()){ $squad[$team] .= [$fn, $ln]; } Then i am wanting to return the output to my page as a list (for ease of the example) with the team at the top and all of the player underneath: Team 1 Player 1 Player 2 Team 2 Player 3 Player 4 foreach($squad as $team){ $out .= "$team"; foreach($team as $key => $val){ $out.="$key $value"; }; }; I thought that the above could help me but i am getting a bit lost.
-
Turns out that i have solved this one myself also, the issue was that i was passing a string in the array and not integers. When i changed the data type to 'is' rather than 'ii' the insert worked.
-
I have created an array that gets passed to php through ajax. case 'createNewGame': exit(createNewGame($_POST['venue'], $_POST['gamedate'], $_POST['home'], $_POST['away'], $_POST['hts'])); break; hts is the array i tried to insert this way function createNewGame($venue, $gamedate, $home, $away, $homeSquad){ $qry = "INSERT INTO game_squad (game_id, player_id) VALUES (?,?)"; $stmt = $conn->prepare($qry); for($i = 0; $i < count($homeSquad); $i++){ $stmt->bind_param('ii', $gameID, $homeSquad[$i]); $stmt -> execute(); } } there is more going on in here with the other variables. When i console log the array before it is passed into php i can confirm i am getting an array: Array [ "20", "21" ] A point in the right direction here would be appreciated.
-
Turned out to be a miss-capitalised bit of data createNewgame should have been createNewGame Sorry all. Thanks for looking anyway.
-
Hi All, I have an ajax call: $('#createNewgame').click(function(){ var errors = []; var gv = $('#gameVenue').val(); var ht = $('#homeTeam').find(":selected").val(); var at = $('#awayTeam').find(":selected").val(); if(gv == '') { $out = "Game Venue"; errors.push($out); } if(ht == 0) { $out = "Home Team"; errors.push($out); } if(at == 0) { $out = "Away Team"; errors.push($out); } if(errors.length != 0){ var finalArray = arrayToStringWithComma(errors) $('.newGameAlertContainer').html("<div class='alert alert-warning newGameAlert hiddenForFade'>"+finalArray+" must be completed!</div>") $('.newGameAlert').fadeIn().delay(3000).fadeOut(1000); return; } if (errors.length == 0){ $.ajax({ type: 'post', data: {'ajax': 'createNewgame', 'venue' : gv, 'home': ht,'away': at }, success: function(resp){ console.log(resp) }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + errorThrown); } }) } }) then i have if(isset($_POST['ajax'])){ switch($_POST['ajax']){ case 'createNewGame': exit(createNewGame($_POST['gv'], $_POST['ht'], $_POST['at'])); break; } } function createNewGame($venue, $home, $away){ include 'includes/dbconn.php'; $qry = "INSERT INTO game (venue, home_team, away_team) VALUES (?,?,?)"; $stmt = $conn -> prepare($qry); $stmt -> bind_param('sii', $venue, $home, $away); $stmt -> execute(); $gameID = mysqli_insert_id(); $_SESSION['activeGame'] = $gameID; return "i ran"; } the success response that i am console logging is the entire html of the whole page. All of this code appears in the same file. Any ideas or pointers? All help appreciated, and have a great new year!
-
Hi All, I have been messing around with some of my projects and have discovered what i assume is quite a regular "issue" in projects. When i have a file in a folder, not in the same folder as most of webpage files, including the header is leading to some issues. For example, if the layout is as follows My Project - home.php - index.php - includes - header.php - testing - test.php - style - custom.css if i include the header on every page with include 'includes/header.php'; and the header includes the style with include './style/custom.css'; on every page that i create in this new testing folder, which is a copy of a page that might exist elsewhere, i am having to change the path of the includes. I have started to look at using the below (which doesnt follow my file strucure) // Predefined path. define('PATH', '/home/ftpuser/public_html/includes/'); include(PATH . 'myscript.php'); i understand what is going on here. Long winded, but my question is. Is the a standard to where these paths should be stored. I dont want to put them in every file, the header file doesnt seem right. Should i create a new file for things like this "paths.php"? Secondly, would people recommend the above or superglobal paths with: // Superglobal path. include(dirname(__FILE__) . '/myscript.php'); I know that the answer to this is likely going to be put them whereever you want, personal preference etc but i am looking for a bit of advice coming from experience about where these would be put in your projects. Thirdly, how would i go about linking css and js files in plain html using these paths, is this possible without opening php inside the link? Thanks as always in advance and have a great new year!
-
Create alert in php and then remove from dom
Adamhumbug replied to Adamhumbug's topic in PHP Coding Help
Thanks for this, ill give it a go. -
HI all, I am looking for some best practice help here. I have a php function called with AJAX and on failure, i would like it to create an alert bar on the page that fades out. I have the know how to show the message on the page but i am doing this using a prepend. I know that this should be handled with JS but i am not sure of the best practice to achive what i am after. Any pointers here appreciated. I have attached the function incase that is useful. function showNewItemAfterBCScan($serial_number, $personId){ include 'includes/dbconn.php'; $out="<div id='assigningKitAlert' class='alert alert-warning'>Bar Code Incorrect</div>"; $stmt = $conn -> prepare(" SELECT ks.id, ks.serial, km.model_name, kf.kit_family from kit_serial ks inner join kit_model km on ks.kit_model = km.id inner join kit_family kf on ks.kit_family = kf.id where ks.serial = ? "); $stmt -> bind_param('i', $serial_number); $stmt -> execute(); $stmt -> bind_result($id, $serial, $mname, $kfamily); if($stmt -> fetch()){ $out = <<<OUTPUT <div class='row bg-secondary m-1 center-all'> <div class='col-2 text-center'> <img class='device-thumb py-3' src='img/DP4800e-Front.png' alt=''> </div> <div class='col-10'> <div class='row'> <div class='col-4'> <h4>$mname</h4> </div> <div class='col-4'> <h4>$kfamily</h4> </div> <div class='col-2'> </div> <div class='col-2'> <h1>x 1</h1> </div> </div> <div class='row'> <div class='col-12'> $serial </div> </div> </div> </div> </div> OUTPUT; assignKit($serial_number, $personId); } $stmt -> close(); return $out; }
-
Html function call on keyup - get keycode from input
Adamhumbug replied to Adamhumbug's topic in Javascript Help
Final solution if it is useful for anyone. function barCodeVal(event){ var e = event.keyCode; var et = event.target if(e === 13){ barCode = et.value console.log(barCode); $.ajax({ type: 'post', data: {'ajax' : 'one', 'barCode': barCode }, success: function(resp){ $('#barCodeOutput').prepend(resp); } }) } } Thanks @Barand -
Html function call on keyup - get keycode from input
Adamhumbug replied to Adamhumbug's topic in Javascript Help
Got ya! got the keycode twice. Thanks as always. -
Html function call on keyup - get keycode from input
Adamhumbug replied to Adamhumbug's topic in Javascript Help
i tried: if($(e).keyCode === 13){ with no joy. -
Html function call on keyup - get keycode from input
Adamhumbug replied to Adamhumbug's topic in Javascript Help
I thought i would be back. I now have the following: function barCodeVal(event){ var e = event.keyCode; console.log(e) if(e.keyCode === 13){ barCode = e.val() console.log(barCode); $.ajax({ type: 'post', data: {'ajax' : 'one', 'barCode': barCode }, success: function(resp){ $('#barCodeOutput').prepend(resp); } }) } } The first console log works and shows 13 as the output. The second console log does not fire suggesting that the if statement is not picking this up. Any helpers appreciated. -
Html function call on keyup - get keycode from input
Adamhumbug replied to Adamhumbug's topic in Javascript Help
I am sorry all, as is always the case i have answered by own question shortly after posting. function barCodeVal(e){ var code = e.keyCode; console.log(e.keyCode); } thanks anyway, i am sure i will be back. -
Hi all, I have an input box that is created dynamically. Before it was created dynamically, it was always in the dom and the following code did what i wanted. $('#serialNumber').keyup(function(e){ if(e.keyCode === 13){ barCode = $(this).val() console.log(barCode); $.ajax({ type: 'post', data: {'ajax' : 'one', 'barCode': barCode }, success: function(resp){ $('#barCodeOutput').prepend(resp); } }) } }) now that it is not part of the dom when the page loads, i am calling it with a key up on the html element. I dont know why i am struggling with this so much but i am really struggling to call the function and pass the keycode into it. I have the following which does give me the keycode but having the second function in the first as it is obviously doesnt do what i want. function barCodeVal(){ $(document).keyup(function(e){ console.log(e.keyCode); }) } I would really appreciate a helping hand on this one - google just shows me react explanations.
-
Multidimentional Arrays [Creating and Looping for output]
Adamhumbug replied to Adamhumbug's topic in PHP Coding Help
Thanks for this, didnt know that was a thing. -
Multidimentional Arrays [Creating and Looping for output]
Adamhumbug replied to Adamhumbug's topic in PHP Coding Help
Thank you for this. Using this method, is there anyway for me to reference the keys rather than the position. So for example when echoing out i could use $id, $ay, $aw?