-
Posts
589 -
Joined
-
Last visited
Posts posted by Adamhumbug
-
-
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
37 minutes ago, Adamhumbug said:data: {'ajax': 'createNewgame', 'venue' : gv, 'home': ht,'away': at
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!
-
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; }
-
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
-
7 minutes ago, Barand said:
e.keyCode ???
Got ya!
got the keycode twice.
Thanks as always.
-
3 minutes ago, Barand said:
e.keyCode ???
i tried:
if($(e).keyCode === 13){
with no joy.
-
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.
-
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.
-
Thanks for this, didnt know that was a thing.
-
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?
-
Perfect answer as always.
That did of course work, as for getting the data out of the other end, could you lend a hand with that also please?
-
Hi All,
I have been scouring the internet for a good source that shows how to create an array and loop through it to output it to the page. I have done this several times but everytime it comes to doing it i have to scratch my head and google to get anywhere near the answer.
I have created a 2 dimentional array of data using the following:
function getExistingAwards(){ include 'includes/dbconn.php'; $stmt = $conn -> prepare("SELECT id, award_name, award_year, award_winner from award"); $stmt -> execute(); $stmt -> bind_result($id, $an, $ay, $aw); $awards = array(); $out = ''; while($stmt -> fetch()){ $awards[$ay] = [$id, $an, $aw]; } }
This gives me a data structure that looks like this.
Array ( [2021] => Array ( [0] => 1 [1] => Avenue Park Award [2] => 1 ) [2019] => Array ( [0] => 2 [1] => Avenue Park Award [2] => 1 ) )
Issue 1 - This looks correct but if there are more than one entries that has the year 2021 for example, only the last one will be added to the array.
Issue 2 - After i have the correct data in the array how do i go about looping through each level of the array. I really struggle with for each loops.
As always, your help is appreciated.
-
Thanks so much and i appreciate you dealing with my rubbish post.
-
prs_ptd_id is a typo - it should be pr_ptd_id
pr_op_id is the id of the person that made the order stored on the order table (prs_pr) and links to op_id in the person table (prs_op)
prs_ptd_id (is the order definition)
pr_o_id is the organisation the order is stored against in the order table (prs_pr) this links to o_id stored in the organiastion table (prs_o)
orders belong to a person and the person belongs to an organsation
-
prs_o table: o_id, o_name
prs_op table: op_id, op_o_id - op_o_id joins to o_id from above
prs_pr table: pr_Id, pr_op_id, prs_ptd_id, pr_ip2ptd_id - pr_op_id joins to op_id from above
-
Hi all,
I have a query that shows me the number of bookings that an organisation has made which works perfectly
select o_id as id, o_name as org, pass_641.media_attendee from prs_o inner join prs_op on o_id = op_o_id inner join (select pr_op_id, pr_ptd_id, count(*) as media_attendee from prs_pr where pr_ptd_id = 641 group by pr_o_id ) as pass_641 on op_id = pass_641.pr_op_id
The data that i get out looks like the below (i have left out the name col)
org count
1 2
2 3
3 9
I want to add another count of other types of order to the query and tried the following
select o_id as id, o_name as org, pass_641.media_attendee, pass_1001.tv from prs_o inner join prs_op on o_id = op_o_id inner join (select pr_op_id, pr_ptd_id, count(*) as media_attendee from prs_pr where pr_ptd_id = 641 group by pr_o_id ) as pass_641 on op_id = pass_641.pr_op_id inner join (select pr_op_id, pr_ip2ptd_id, count(*) as tv from prs_pr where pr_ip2ptd_id = 1001 group by pr_o_id ) as pass_1001 on op_id = pass_1001.pr_op_id
This only shows me organisations that have both of the order types rather than showing NULL if they have one and not the other - this is clearly to do with the inner joins.
I tried changing to left joins but this shows a row per order like the below
org count count
1 2 NULL
1 NULL NULL
I would appreciate a point in the right direction with this.
To confirm if i run this with only one order type the individual queries run correctly.
-
As always - thank you so much!
-
I am in 100% agreement, i am afraid the stuff i am doing at the minute is not on my database and it is unlikely to get any better in the short term.
So i have a person table with:
id - fname - lname
1 - Adam - Smith
2 - John - Hughes
I have a badge table with:
badgeid - personid - zones
1 - 1 - 101,210,301,401,501,601
2 - 2 - 301,601,1101
and i need to get a list together of everyone that has more than one of the zones 301,401,501.
Hopefully this does not require something as wonderful as the function in the last "mess tidying series of SQL statements"
returning the result of multidimentional array
in PHP Coding Help
Posted
I do have one little issue with this.
I have the following
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.