PHP - My Code Is Not Doing The Result I Want It Too
Hey Everyone,
I am posting today because I have a site I am working on which uses the xzero classifieds and having a small problem accomplishing something with it. I am trying to relocate the ad count number (of the city the user is currently browsing) to a box in my sidebar which has various site statistics like the following Online Users: XX Ads in this city: 0 But my code keeps giving me a 0 (even after I posted some test ads) Anywho, heres what I have so far but cant seem to make it work <?php $country_adcounts = array(); $city_adcounts = array(); $sql = "SELECT ct.cityid, c.countryid, COUNT(*) as adcnt FROM $t_ads a INNER JOIN $t_cities ct ON ct.cityid = a.cityid AND ($visibility_condn) INNER JOIN $t_countries c ON ct.countryid = c.countryid WHERE ct.enabled = '1' AND c.enabled = '1' GROUP BY ct.cityid"; $res = mysql_query($sql) or die(mysql_error().$sql); while($row=mysql_fetch_array($res)) { $country_adcounts[$row['countryid']] += $row['adcnt']; $city_adcounts[$row['cityid']] += $row['adcnt']; } $adcount = 0+$city_adcounts[$city['cityid']]; ?> and this is the code which should recall the adcount <?php echo $adcount; ?> What am I doing wrong? I'm placing the first code on my sidebar and attempting to call the ad count through an echo. So ya, if anyone could tell me why this isn't working or lend a hand in helping me fix it please know I'll greatly appreciate it. Thanks. Similar TutorialsAt present, all my queries return multiple records. Now I want to do a record count, and only return the one record. Current Code Code: [Select] $query = "SELECT * FROM tblListField WHERE ListID = '" . $_SESSION['listid'] . "'"; $result = mysqli_query($dbc, $query) or die('Error executing SELECT * statement for tblListField - ' . $query); if(mysqli_num_rows($result)) { // at least one row, process the data from the query while ($row = mysqli_fetch_array($result)) { So the above runs through each record; what do I use to get the one record from the following SQL? (Note; I'm using mysqli_) Code: [Select] $query = "SELECT COUNT(ItemField_1) WHERE ListID='" . $listid . "'"; I have a database that contains results of fantasy football games. I have a query that returns the results of the games team X played against the other teams in the league. The query works perfect in the database. Results are Quote Opponent Name: America Enforcers Wins Losses Draws 1 1 0 Last Game: Week 2 of 2011: 416.6 - 369.8 the query and the php code cycle thru each team that team X has played and brings up team X's record and the results of the last game played. My problem is that the code stops one team short. So Team X played teams A, B, C, D, E but the code only returns teams A, B, C, D E is left off. I can't for the life of me figure out what silly mistake I made. Code: [Select] <?php include_once('../other/functions.php'); $con = mysql_connect($hostname, $username, $password) OR DIE ('Unable to connect to database! Please try again later.'); $db = mysql_select_db($dbname, $con); $thing = $_GET['thing']; $query = "select selected.teamname AS selected_team, selected_score.score AS selected_score, week.week, year, home_id, ". "target_score.score as target_score, target.teamname as targetname, week.ID ". "from owners as selected ". "JOIN game_scores AS selected_score ON selected.owner_id = selected_score.team_id ". "JOIN game_setup ON game_setup.game_id = selected_score.game_id ". "JOIN game_scores AS target_score ON target_score.game_id = game_setup.game_id AND target_score.team_id != selected_score.team_id ". "JOIN owners AS target ON target.owner_id = target_score.team_id ". "JOIN week ON week.week = game_setup.week ". "WHERE selected.owner_id = $thing ". "and target.active = 1 ". "GROUP BY target.teamname, year, week.ID "; $result = mysql_query($query); $row = mysql_fetch_array($result); $result1 = mysql_query($query); $row1 = mysql_fetch_array($result1); if (!$result) { die('Invalid query: ' . mysql_error()); } $wins=0; $losses=0; $draws=0; $last_target = false; echo '<h2>' . $row1['selected_team'] . ' vs. Active Teams</h2>'; while ($row = mysql_fetch_assoc($result)) { if ($last_target['targetname'] != $row['targetname']) { if ($last_target) { printf(' <table border="1" width="600"> <tr> <th>Opponent Name:</th> <th colspan="2">%s</th> </tr> <tr> <th>Wins</th> <th>Losses</th> <th>Draws</th> </tr> <tr> <td align="center">%d</td> <td align="center">%d</td> <td align="center">%d</td> </tr> <tr> <th>Last Game:</th> <td colspan="2">%s of %s: %.1f - %.1f</td> </tr> </table><br/>', $last_target['targetname'], $wins, $losses, $draws, $last_target['week'], $last_target['year'], $last_target['selected_score'], $last_target['target_score'] ); } $wins = $losses = $draws = 0; } if ($row['selected_score'] < $row['target_score']) ++$losses; elseif ($row['selected_score'] == $row['target_score']) ++$draws; else ++$wins; $last_target = $row; } ?> Any ideas on how to make the magic elvish be good??? Thank you for your time. This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=343031.0 I am really lost here with this date issue of mine. The below code is the last part of a query: Code: [Select] $defendercheck_3 = $row_checkifattacked3['atdate']; $defendercheck1_3 = strtotime("$defendercheck_3"); $defendercheck2_3 = date("D", $defendercheck1_3); The query does not return any results as expected, but when echoing the various steps I get following: echo "$defendercheck3"; = nothing (as expected) echo "$defendercheck1_3"; = nothing (as expected) echo "$defendercheck2_3"; = result! (NOT expected) why does it return anything on "date("D", $defendercheck1_3)" when "$defendercheck1_3" is blank? at the moment i have my page: addevent.php i have written: Code: [Select] $q = "SELECT username " ."FROM ".TBL_USERS." ORDER BY ASC"; $result = $database->query($q); on the database.php page here is what query looks like: Quote /** * query - Performs the given query on the database and * returns the result, which may be false, true or a * resource identifier. */ function query($query){ return mysql_query($query, $this->connection); } then i want to display $result in a drop down menu so i wrote back on addevent.php: Code: [Select] <select name="Select1"> <option value="<? echo("$result"); ?>"><? echo("$result"); ?></option> </select> Basically I want this to do do is: if (there is no result found in the query){ echo "nothing for this brand"; }else{ echo this link; } this is my code...im just confused on what i want the if statement to to ask for. any help would be greatly appreciated Code: [Select] <?php $mypro = mysql_query("SELECT * FROM Sheet1 WHERE pro_catagory LIKE '%People%' ORDER BY pro_id DESC"); while($row=mysql_fetch_array($mypro)) { if(Not sure what to put here){ echo "There are no listings for the brand right now"; }else{ print "- <a href='proList.php?proNumber=".$row['pro_id']."'>".$row['pro_name']."</a><br />"; } } ?> I'm writing an admin panel for my site (to manage users, etc) I selected the user-rights from the DB, but how can I check it the user-rights is equal to 3, and if it is allow access? Ok i call a function like so ... <?php PunchTime($monday,"in","1",$etime_who); ?> Below is the function, if you look closely i echo the query after it's been ran, and that query works 100% since i copied & pasted the query into the database and it returns a result that's needed. But for some bloody reason it doesn't work via code. function PunchTime($pdate,$type,$num,$ewho) { if($type == "in") { if($num == "1") { $etime_col = "PUNCH_IN"; } elseif($num == "2") { $etime_col = "PUNCH_IN2"; } } elseif($type == "out") { if($num == "1") { $etime_col = "PUNCH_OUT"; } elseif($num == "2") { $etime_col = "PUNCH_OUT2"; } } include_once('oracleCON.php'); $etime_qry = "SELECT $etime_col FROM tablename WHERE who= '$ewho' AND punch_date = '$pdate'"; echo $etime_qry; $etime_go = oci_parse($conn, $etime_qry); oci_execute($etime_go); $etime_res = oci_fetch_assoc($etime_go); $time = $etime_res[$etime_col]; if($time == "") $time = "--"; echo $time; } All i get are "--" even though some should return a time stamp. i have a game script and it's always bringing back the same result and i cant see why.. all i get when the script it ran is [/code]echo '<div class=noticeBox><h2>Sorry! No available member found![/code] the code is below and can provide the functions and db if needed, but cant see what's wrong. Code: [Select] <?php include '../header.php'; echo '<div id=AjaxSubDiv>'; echo '<div id="actionDiv">'; include '../statusbar.php'; echo '<div id=contentBox>'; // Show Fight Result if( !empty($_GET[action]) AND $_GET[action]=='attack' ) { echo $str; // Fight Lists echo '<P class=pageheader>Fight</P>'; echo '<P>Its time for action! Fight with rival thieves and grab their cash. You will require 1 strength for each fight and you will gain some cash and exp if you win the fight. However you will just earn some exp if you loose!! Each fight will decrease your health so keep an eye on health level because if your health goes below zero you will die and loose everything...</P>'; $res = query("SELECT COUNT(*) FROM `usertable` WHERE `userid`!=$user"); list($total) = mysql_fetch_array($res); if( $total>0 ) { $res = query("SELECT * FROM `usertable` WHERE `userid`!=$user LIMIT 0,20"); $viewplayer = Player::getById($row[userid]); echo '<tr>'; echo '<td width=120px>'; echo '<P class=titleP><a href="#" onclick="load_menu_val(\'AjaxSubDiv\', \'profile\', '.$row[userid].');return false;"><fb:profile-pic uid="'.$row[userid].'" size="square" linked="false" width="75px" class="userImg" /></a></P>'; echo '<P class=titleP><a href="#" onclick="load_menu_val(\'AjaxSubDiv\', \'profile\', '.$row[userid].');return false;"><fb:name uid="'.$row[userid].'" linked="false" /></a></P>'; echo '</td>'; echo '<td width=120px>'; echo '<P><B>Level:</B> '.$viewplayer->level.', '.get_level_title($player->level).'</P>'; echo '</td>'; echo '<td width=120px>'; echo '<P><B>Crew:</B> '.$viewplayer->crew.'</P>'; echo '</td>'; echo '<td width=300px>'; if( $viewplayer->health > 20 ) { echo '<form id="FightForm'.$row[userid].'" method="POST" class="itemForm">'; echo '<input type=hidden name=user value="'.$user.'">'; echo '<input type=hidden name=opid value="'.$row[userid].'">'; echo '<input type=hidden name=action value="dofight">'; echo '<P><input type=button onclick="do_submit(\'FightForm'.$row[userid].'\',\'http://www.bofs.us/thief/thiefmaster/fight/post_fight.php\', \'actionDiv\'); return false;" value="Fight Now" class="submitButton" /></P>'; echo '</form>'; } else { echo '<P class=titleP>Too Weak To Fight</P>'; } echo '</td>'; echo '</tr>'; } echo '</table>'; echo '</center>'; } else { echo '<div class=noticeBox><h2>Sorry! No available member found!</h2></div>'; print"$res"; } echo '</div>'; // end of contentBox echo '</div>'; // end of actiondiv echo '</div>'; // end of AjaxSubDiv ?> i have it set up so the user picks the year and session and then the database shows all the games in that specific year and session and then displays them with a link to their photo galleria and also shows the record(wins - losses - ties) but the way i have it know its not displaying the first result here is the code Code: [Select] <?php //declares $y as year played and $s as session $y = ' '; $s = ' '; //handles submition of form for picking year and session if (isset($_POST['submit'])) { $data = mysql_real_escape_string($_POST['session']); $exploeded = explode(",", $data); $y = $exploeded[0]; $s = $exploeded[1]; } //Query for the selection menu $squery = "SELECT DISTINCT(Year_Played), Sessions FROM pinkpanther_games"; $sresults = mysql_query($squery) or die("squery failed ($squery) - " . mysql_error()); //checks $y and $s to see if their is a vaule if ($y == ' '){$y = '20112012';} if ($s == ' '){$s = '2';} //Query for getting record and for getting all the appropriate info for displaying the games $query = " SELECT (SELECT COUNT(id) FROM pinkpanther_games WHERE Year_Played = $y AND Sessions = $s AND Win_Loss = 'Tie' ) AS 'Tie', (SELECT COUNT(id) FROM pinkpanther_games WHERE Year_Played = $y AND Sessions = $s AND Win_Loss = 'Win') AS 'Win', (SELECT COUNT(id) FROM pinkpanther_games WHERE Year_Played = $y AND Sessions = $s AND Win_Loss = 'Loss') AS 'Lose', Opponent AS Opponent, Score AS Score, Gallery_no AS Gal, Win_Loss AS Record FROM pinkpanther_games WHERE Year_Played = $y AND Sessions = $s"; $results = mysql_query($query) or die("Query failed ($query) - " . mysql_error()); $row_a = mysql_fetch_assoc($results); //Set record variables $wins = $row_a['Win']; $loss = $row_a['Lose']; $tie = $row_a['Tie']; //creates from and selection menu for year and session echo "<tr><td colspan='2'><form method='post' action=''><select name='session' class='txtbox2'>"; while ($srow = mysql_fetch_assoc($sresults)){ echo "<option value='" . $srow['Year_Played'] . "," . $srow['Sessions'] . "'>" . $srow['Year_Played'] . " Session " . $srow['Sessions'] . "</option>"; } echo "</select><input type='submit' name='submit' value='Go' class='txtbox2' /></form></td></tr>"; //displays record echo "<tr><td colspan='2'>" .$wins . " - " . $loss . " - " . $tie . "</td></tr>"; echo "<tr></tr>"; //displays games and scores in appropriate year and session while ($row = mysql_fetch_assoc($results)){ $opp = $row['Opponent']; $score = $row['Score']; $gal = $row['Gal']; $wlt = $row['Record']; //styles games acording to if win lose or tie if ($wlt == 'Win'){ echo "<tr><td class='win'>"; echo "<a href='galleries.php?g=$gal'" . $gal . " >" . $opp . "</a>"; echo "</td><td class='win'>"; echo $score . ""; echo "</td></tr>"; } if ($wlt == 'Loss'){ echo "<tr><td class='loss'>"; echo "<a href='galleries.php?g=$gal'" . $gal . " >" . $opp . "</a>"; echo "</td><td class='loss'>"; echo $score . ""; echo "</td></tr>"; } if ($wlt == 'Tie'){ echo "<tr><td class='tie'>"; echo "<a href='galleries.php?g=$gal'" . $gal . " >" . $opp . "</a>"; echo "</td><td class='tie'>"; echo $score . ""; echo "</td></tr>"; } } ?> hello, anybody able to help me with why this is only returning the first staff member's hours? Code: [Select] <?php if(isset($_POST['view'])) { $y3=$_POST['y']; $m3=$_POST['m']; $d3=$_POST['d']; $pdate=$y3."-".$m3. "-".$d3; $pdate1 = date( 'D M j', strtotime($pdate) ); } else { $pdate = date('Y-m-d', strtotime("-1 day") ); $pdate1 = date( 'D M j', strtotime($pdate) ); } echo "<table border='1' style='border-collapse: collapse' bordercolorlight='#000000' bordercolordark='#000000' width='98%' align='center'>"; echo "<tr><td width='100%' colspan='9' align='center'><b>Timesheets For $pdate1</b></td></tr>"; $result = mysql_query("SELECT * FROM staff ORDER BY name"); while($row = mysql_fetch_array($result)) { $eid=$row['eid']; $name=$row['name']; echo "<tr>"; echo "<td align='left' colspan='9'><b>" . $name . "</b></td>"; echo "</tr>"; echo "<tr> <th align='center'>Date</th> <th align='center'>Job Number</th> <th align='center' width='30%'>Service Report</th> <th align='center'>Sign In Time</th> <th align='center'>Sign Out Time</th> <th align='center'>Lunch</th> <th align='center'>Time Billed</th> <th align='center'>Estimated</th> </tr>"; $result3 = mysql_query("SELECT * FROM timesheet WHERE date = '$pdate' AND eid = '$eid'"); $talltime = 0; while($row3 = mysql_fetch_array($result3)) { $tid=$row3['id']; $tdate=$row3['date']; $tjobnumber=$row3['jobnumber']; $teid=$row3['eid']; $tdescription=$row3['description']; $tsignin=$row3['start']; $tfinish=$row3['finish']; $tlunch=$row3['lunch']; $tkms=$row3['kms']; $tschednum=$row3['schednum']; $tdate1 = date( 'M j, Y', strtotime($tdate) ); $tsignin1 = date( 'g:i a', strtotime($tsignin) ); $tfinish1 = date( 'g:i a', strtotime($tfinish) ); if( empty($tfinish) ) { $tfinish2="<i>In Progress"; } else { $tfinish2="$tfinish1"; } $shortid=substr($tjobnumber, 5, -1); //remove the first 5 characters and minus the last character $result = mysql_query("SELECT * FROM jobs WHERE id = '$shortid'"); while($row = mysql_fetch_array($result)) { $jstatus=$row['status']; } $result4 = mysql_query("SELECT * FROM schedule WHERE id = '$tschednum'"); while($row4 = mysql_fetch_array($result4)) { $sid=$row4['id']; $sdate=$row4['date']; $seid=$row4['eid']; $sjobnumber=$row4['jobnumber']; $sstarttime=$row4['starttime']; $sstatus=$row4['status']; $setime=$row4['etime']; } $log_in_time_string = strtotime($tsignin); $log_out_time_string = strtotime($tfinish); $difference_in_seconds = ($log_out_time_string - $log_in_time_string); $tbilled = ($difference_in_seconds / 3600); if($tbilled < 0) { $tbilled1 = $tbilled + 24; } else { $tbilled1=$tbilled; } $tbilled2 = number_format(round($tbilled*4)/4,2); $tbilled3 = $tbilled2 - $tlunch ; $talltime += $tbilled3; echo "<tr>"; echo "<td align='center'>" . $tdate1 . "</td>"; echo "<td align='center'>" . $tjobnumber . "</td>"; echo "<td align='center'>" . $tdescription . "</td>"; echo "<td align='center'>" . $tsignin1 . "</td>"; echo "<td align='center'>" . $tfinish2 . "</td>"; echo "<td align='center'>" . $tlunch . " hour</td>"; echo "<td align='center'>"; if ($tbilled3 > $setime ) { echo "<font color='red'><b>*** " . $tbilled3 . " hours ***</b></font>"; } else { echo "" . $tbilled3 . " hours"; } echo "</td>"; echo "<td align='center'>" . $setime . " hours</td>"; echo "</tr>"; } echo "<tr>"; echo "<td colspan='6' align='right'><b>Totals :</td>"; echo "<td align='center' colspan='2'>"; if ($talltime > "8" ) { echo "<font color='red'><b>*** " . $talltime . " hours ***</b></font>"; } elseif ($talltime < "0" ) { echo "<font color='red'><b>Not Signed Out</b></font>"; } else { echo "" . $talltime . " hours"; } echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td align='center' colspan='8' bgcolor='#D9FFD9'><hr></td>"; echo "</tr>"; } echo "</table>"; include 'close.php'; ?> I have a list of ID's returned from a table (even number). I always used a loop to get the next result, but I want to put these straight into a new table INSERT INTO table VALUES ($ID1, $ID2); This is recursive till there are no more ID's. How do I get the result then the next result without loops. ID1 and ID2 cannot be null. What is the best way? $name = $name . "<option value=". $row['name'] . ">" .$row['name'] . "</option>"; but the name is "Mr. Robert",it does have dot symbol...how to overcome? please help Hi, I was wondering if I wanted lets say 3 to the power of 10 how can I do this in php? I searched a few places for some power to operator in PHP but no luck could some one tell me thanks? Good Day, I'm struggling to get strtotime() to return anything. I have the following code.. $fields['birth']['day'] = 9; $fields['birth']['month'] = 7; $fields['birth']['year'] = 1982; $dob = $fields['birth']['day'].' '.$fields['birth']['month'].' '.$fields['birth']['year']; // Returns: 9 7 1982 $dob = strtotime($dob); die($dob); No matter what I do $dob isn't returning anything once strtotime() is used. I've tried matching my day/month/year 's with examples in the manual on the strtotime() page but using date() on the fields, but it still doesn't want to return anything. I feel I'm missing something simple here. Any help is appreciated. Regards, Ace I have been looking at this code all day trying to find where I had results and didn't free them. Hopefully your eyes will have better luck. // if form is filled continue with script if($full == "TRUE"){ // function to concatinate two individuals string into one string function fullname($first, $last){ $fullname = "$first"." "."$last"; return $fullname; } $fullname = fullname($_POST['firstname'], $_POST['lastname']); // Check for existing user in rak5 $sql = "SELECT COUNT(*) FROM rak5 WHERE empid = '$_POST[empid]' AND fullname = '$fullname'"; $rakfcheck = mysql_query($sql); if (@mysql_result($rakfcheck,0,0) > 0) { mysql_free_result($rakfcheck); // check uaid in user table $sql = "SELECT COUNT(*) FROM user WHERE UAID = '$_POST[UAID]'"; $UAIDcheck = mysql_query($sql); //if UAID is in the user table then check employee id for redundancy if(@mysql_result($UAIDcheck,0,0) > 0){ mysql_free_result($UAIDcheck); $sql = "SELECT COUNT(*) FROM user WHERE UAID = '$_POST[UAID]' AND employeeID = '$_POST[empid]'"; $UAID_Empcheck = mysql_query($sql); //if there is a UAID emplid match on the user table print redundancy //else UAID is occupied by another user mysql_free_result($UAID_Empcheck); error(' Employee already has a UAID!'); } else { error('UAID is currently active by a different employee'); } } //UAID is not in the user table else{ $newpass = substr(md5($_POST['empid']),0,6); //insert new user $sql = "INSERT INTO user SET userid = '', firstname = '$_POST[firstname]', lastname = '$_POST[lastname]', email = '$_POST[newemail]', notes = '', password = ('$newpass'), UAID = '$_POST[UAID]', BOG = '', employeeID = '$_POST[empid]'"; $result = mysql_query($sql); // Email the new password to the person. $message = "G'Day! Your personal account for the Project Web Site has been created! To log in, proceed to the following address: http://ciwdvweb1.itd/web2/test2/index.php Your personal login ID and password are as follows: userid: $_POST[UAID] password: $newpass You aren't stuck with this password! Your can change it at any time after you have logged in. If you have any problems, feel free to contact me at Security Front End GUI creator "; $umail = $_POST['newemail']; mail($umail,"UAID and password", $message, "From: <you@example.com>"); include "MoveOn.html"; } } else{ error('either the name or employee id entered is incorrect please resubmit'); } } else { error('Please fill out the whole form'); } mysql_close(); endif; ?> I was wondering if it was my INSERT query but I tried to save the query into a variable and free it but I got another warning saying the variable wasn't the right parameter. Thank you for your help Hi i got a search form and when im searhing for result i get nothing. i got 5 search fields : 1. Erea 2. category 3. subcategory 4. from price 5. to price if im not specifieing from price and to price i get blank result ,if i do specify the from price to price ,then i get the results. this is the code im using to get the results. Code: [Select] $sql=mysql_query("select * from posts where erea = '$erea' and category = '$category' and sub_category = '$subcategory' and price >= '$fromprice' and price <= '$toprice'"); what i want is to get the result even if i didnt specify the price from, to . thanx How can I add a link when my query result is >= 6 ? Do I have to use mysql_num_rows? Hello all. I am using a premade login script which has some very powerful function built in, the one I am looking at using is as follows:
/** * query - Performs the given query on the database and * returns the result, which may be false, true or a * resource identifier. */ function query($query){ return mysqli_query( $this->connection, $query); }The issue is, displaying the results. I call is via the following code: $database->query('SELECT LastName, Firstname FROM Patrons WHERE LastName = "Delude"');But I have no idea how to display it. I have attempted to echo it out, but I get the following error in the logs: PHP Catchable fatal error: Object of class mysqli_result could not be converted to string |