
Drummin
Members-
Posts
1,004 -
Joined
-
Last visited
Everything posted by Drummin
-
There are many problems in your inline php and html. IF no results are found you start with a <tr>. This is fine assuming a <table> tag is above this. However if there are results you start with a <table> tag, which I assume will result in having double <table><table> tags. You are also not consistent in the number of columns for each row. Much of the inline php is not right,i.e missing <? from <?php and using full quote for fields. In the result loop you need to include the opening <tr> tag so as this loops a new line is created. I've attempted to fix SOME table issues, commenting out the second table and making a join query though I'm only guessing the fields are what you need and it's formatted correctly for you. I also wrapped this whole section in an IF statement so it is contained by the $_GET['modno']. .I would suggest setting up a test page before replacing your code. if (isset($_GET['modno'])){ $modno = ((isset($_GET['modno']) && is_numeric($_GET['modno'])) ? trim($_GET['modno']) : 0); $SQLstmt = "SELECT m.modelName, m.modelID, m.modelNo, i.imageFile"; $SQLstmt .= " FROM tblModel as m"; $SQLstmt .= " LEFT JOIN tblImages as i"; $SQLstmt .= " ON m.modelID = i.modelID"; $SQLstmt .= " WHERE m.modelNo = $modno"; $db=mysql_query($SQLstmt,$oConn) or die(mysql_error()); if (mysql_num_rows($db)==0){ $countData="No results found of selected query"; ?> <tr> <td colspan="5" class="text9"><br> <strong>Search Result</strong> </td> </tr> <tr> <td class="text" colspan="5"> <?php echo $countData; echo $db; echo mysql_num_rows($db); ?></td> </tr> <tr> <td colspan="5" height="4"><hr /></td> </tr> <?php }else{ ?> <!-- <table width="100%" align="center" cellpadding="0" cellspacing="0"> --> <tr> <td class="text9"><br><strong>Search Result</strong></td> <td colspan="4" align="right"></td> </tr> <tr> <td width="150" class="text"><div align="center"><strong>Thumbnail</strong></div></td> <td width="150" class="text"><strong>Model No</strong></td> <td width="75" colspan="3"></td> </tr> <tr> <td colspan="5" height="4"><hr /></td> </tr> <?php while ($line = mysql_fetch_array($db)){ ?> <tr> <td class="text" colspan="3"><div align="center"><a href="../models/model.php?modelID=<?php echo $line['modelID']; ?>#"><img src="../cms/models/lineup/<?php echo $line['modelID']; ?>/<?php echo $line['imageFile']; ?>" height="100" width="<?php echo $width; ?>" border="0"></a></div></td> <td width="147" class="text"><?php echo $line['modelNo']; ?></td> <td width="78" class="text"><a href="../models/model.php?modelID=<?php echo $line['modelID']; ?>#">View</a></td> </tr> <tr> <td colspan="5" height="4"><hr /></td> </tr> <?php } } } ?>
-
Can you post your current $_GET['modno'] query and explain again what the problem is? You should list needed field names from each table as well.
-
Yes it looks like you are using the same variable $SQLstmt for the GET query and the search query. Try changing one of them to $SQLstmt2 and see how it goes.
-
Agreed, as you have it now, $_GET['modno']=21 would result in $modno=212121. As scootstah suggested a single line should be used. $modno = ((isset($_GET['modno']) && is_numeric($_GET['modno'])) ? trim($_GET['modno']) : 0); As far as your query, besides being a waste, using * is a bad idea especially if there are duplicate field names, in which case the query will probably fail. But say you don't have duplicate fields, I would think a LEFT JOIN would be a better option. In either case listing needed field names for each table would be better.
-
radio button option posted to a backend php program
Drummin replied to helen11's topic in PHP Coding Help
Sounds like homework. In any case, your two radio button sets need to processed as either/or because you can't for example show the highest while also getting the sum. For this reason, I would name them all the same. You have radio buttons above and below text inputs and so to make the display less confusing, I would place them all at the top and label them as a group side-by-side. A few floating divisions should do the job. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Simple Calculator</title> </head> <body> <h1>Simple Calculator</h1> <h2>Please Select an Option</h2> <form method="post" action="calculation.php"> <div style="float:left;"> <div style="float:left; padding-right:10px"> <h3>Sort Options</h3> <input type="radio" name="calc" value="Highest" /> Highest<br /> <input type="radio" name="calc" value="Lowest" /> Lowest<br /> <input type="radio" name="calc" value="Average" /> Average<br /> <input type="radio" name="calc" value="Sort in ascending order" /> Sort in ascending order<br /> </div> <div style="float:left;"> <h3>Calculation Options</h3> <input type="radio" name="calc" value="add" /> add<br /> <input type="radio" name="calc" value="subtract" /> subtract<br /> <input type="radio" name="calc" value="multiply" /> multiply<br /> <input type="radio" name="calc" value="divide" /> divide<br /> </div> </div> <div style="float:left; clear:both;"> <h2>Insert five numbers in the form and hit submit button</h2> <p>Value 1: <input type="text" name="val1" size="10" /></p> <p>Value 2: <input type="text" name="val2" size="10" /></p> <p>Value 3: <input type="text" name="val3" size="10" /></p> <p>Value 4: <input type="text" name="val4" size="10" /></p> <p>Value 5: <input type="text" name="val5" size="10" /></p> <p><input type="submit" name="submit" value="Calculate" /></p> </div> </form> </body> </html> For processing, it's better to use if (empty($_POST['val1']) rather than if ($_POST[val1] == "") Also note, as already mentioned how the $_POST keys are in single quotes, i.e. ['val1']. When dealing with radio buttons, unless you've added a default selection as blank... <input type="radio" name="calc" value="" checked="checked" /> Choose ...then checking for blank is pointless. $_POST[calc] =="" Instead check if the value isset(). if (isset($_POST['calc'])){ header() should always be called before anything is sent to the browser, ex. <head> etc. and in general it's a good practice to always do any post-processing before output, so get in the habit of setting up your pages this way. In fact in a case like this project, I would put processing above <html> on the same page and make the form fields sticky (echo post values back) so it the user forgets to make a selection, the values are still in the form and you can display a message like "Please select a processing option". As far as the sorting options, I think adding posted values to an array then sorting is the way to go. Because I have processing above <body> tag, I will format result output as a variable and echo within body. So in any case, here's my version. <?php if (empty($_POST['val1']) || empty($_POST['val2']) || empty($_POST['val3']) || empty($_POST['val4']) || empty($_POST['val5'])) { header("Location: form.html"); exit; } if (!isset($_POST['calc'])){ header("Location: form.html"); exit; }else{ if (isset($_POST['calc'])){ if ($_POST['calc'] == "add") { $result = $_POST['val1'] + $_POST['val2'] + $_POST['val3'] + $_POST['val4'] + $_POST['val5']; } elseif ($_POST['calc'] == "subtract") { $result = $_POST['val1'] - $_POST['val2'] - $_POST['val3'] - $_POST['val4'] - $_POST['val5']; } elseif ($_POST['calc'] == "multiply") { $result = $_POST['val1'] * $_POST['val2'] * $_POST['val3'] * $_POST['val4'] * $_POST['val5']; } elseif ($_POST['calc'] == "divide") { $result = $_POST['val1'] / $_POST['val2'] / $_POST['val3'] / $_POST['val4'] / $_POST['val5']; } elseif ($_POST['calc'] == "Highest") { $total= array(); $total[] =$_POST['val1']; $total[] =$_POST['val2']; $total[] =$_POST['val3']; $total[] =$_POST['val4']; $total[] =$_POST['val5']; $result = max($total); } elseif ($_POST['calc'] == "Lowest") { $total= array(); $total[] =$_POST['val1']; $total[] =$_POST['val2']; $total[] =$_POST['val3']; $total[] =$_POST['val4']; $total[] =$_POST['val5']; $result = min($total); } elseif ($_POST['calc'] == "Average") { $result = (($_POST['val1'] + $_POST['val2'] + $_POST['val3'] + $_POST['val4'] + $_POST['val5'])/5); } elseif ($_POST['calc'] == "Sort in ascending order") { $numbers= array(); $numbers[] =$_POST['val1']; $numbers[] =$_POST['val2']; $numbers[] =$_POST['val3']; $numbers[] =$_POST['val4']; $numbers[] =$_POST['val5']; asort($numbers); $result = implode(', ',$numbers); } } } $display =""; $display .="<p>The result of the calculation is: $result</p>"; $display .="<p><a href=\"form.html\" target=\"_self\">Do Another</a></p>"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Calculation Result</title> </head> <body> <?php if (isset($display)){ echo "$display"; } ?> </body> </html> -
Field with Null not returned
Drummin replied to doubledee's topic in Editor Help (PhpStorm, VS Code, etc)
I do see that dump shows NULL where print does not if that helps at all. <?php $s_birthYear=NULL; echo "print:<br />"; print_r($s_birthYear); echo "<br />dump:<br />"; var_dump($s_birthYear); ?> -
Redesign so processing is before <html>. Can't have header() after anything sent to browser.
-
Seems like this should work. <?php $cats = mysql_query("SELECT cid, cname FROM category WHERE cparent ='4' ORDER BY cid DESC"); while ($data = mysql_fetch_array($cats)) { $cname = $data['cname']; $seo_link = seo($cname); $result = mysql_query("UPDATE category SET cseo='$seo_link' WHERE cid='".$data['cid']."'"); } ?>
-
Correct, $data['cname'] is not an array. What is seo($seo_names)?
-
I would suggest making each <select name="operator"> a unique name, operator1 operator2 etc and handle POST accordingly. OR only have ONE operator selection. Also you're not passing values for your options. <option value="+">+</option>
-
A little update to the TOP of my version. $y = date("Y"); if (isset($_POST['month'])){ $m=$_POST['month']; $cy=$_POST['year']; $viewmonth = "$m-1-$cy"; }else{ $m = date("n"); $y = date("Y"); $viewmonth = date("n-j-Y",strtotime("+0 months")); }
-
Ahh for the fun of it. I'm sure the BIG BOY's could do better but it was fun non the less. <?php if (isset($_POST['month'])){ $m=$_POST['month']; $y=$_POST['year']; $viewmonth = "$m-1-$y"; }else{ $m = date("n"); $y = date("Y"); $viewmonth = date("n-j-Y",strtotime("+0 months")); } list($thisMonth,$thisDay,$thisYear) = preg_split('/[\/\-\ \,\.]/', $viewmonth); $daysinmonth=date("t", mktime(0,0,0,$thisMonth,$thisDay,$thisYear)); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> .table{ background-color:#ddd; font-family:arial; font-size:12px; } </style> </head> <body> <form action="" method="post"> <select name="month" onchange="this.form.submit()"> <?php $months = array(1=>"January", 2=>"February", 3=>"March", 4=>"April", 5=>"May", 6=>"June", 7=>"July", 8=>"August", 9=>"September", 10=>"October", 11=>"November", 12=>"December"); foreach ($months as $month =>$shown_month){ if (isset($_POST['month'])){ $selected_month=($_POST['month']==$month ? 'selected="selected"':''); }else{ $selected_month=($m==$month ? 'selected="selected"':''); } echo "<option value=\"$month\" $selected_month>$shown_month</option>"; } ?> </select> <select name="year" onchange="this.form.submit()"> <?php foreach(range($y,1960) as $year){ if (isset($_POST['year'])){ $selected_year=($_POST['year']==$year ? 'selected="selected"':''); }else{ $selected_year=($y==$year ? 'selected="selected"':''); } echo "<option value=\"$year\" $selected_year>$year</option>"; } ?> </select> </form> <?php $i=1; $ii=1; echo"<table class=\"table\">\r"; echo "<colgroup style='background-color:#efefef'></colgroup>\r"; while ($ii<($daysinmonth+1)){ $fd2 = date("D", mktime(0, 0, 0, $thisMonth, $ii, $thisYear)); if ($fd2 == 'Sat' OR $fd2 == 'Sun'){ echo "<colgroup style='background-color:#C6C7CB'></colgroup>\r"; } else{ echo "<colgroup style='background-color:#ffffff'></colgroup>\r"; } $ii=$ii+1; } echo "<tr style=\"font-size:.9em;\">\r<th>ASEAN Reports</th>\r"; while ($i<($daysinmonth+1)){ $fd = date("D", mktime(0, 0, 0, $thisMonth, $i, $thisYear)); if ($fd == 'Sat' OR $fd == 'Sun'){ echo "<th align=\"center\" width=\"25\" style='background-color:#dcdcdc'>$fd<br />$i</th>\r"; } else{ echo "<th align=\"center\" width=\"25\">$fd<br />$i</th>\r"; } $i=$i+1; } echo "</tr>\r"; $fakerecords=array('Country','Reports','Report1','Report2'); foreach($fakerecords as $record){ echo "<tr>\r<td align=\"left\">$record</td>\r"; for ($d=1; $d<=$daysinmonth; $d++){ //DB query could be here for current date and link added to this cell echo "<td> </td>\r"; } echo "</tr>\r"; } echo "</table>\r"; ?> </body> </html>
-
Update new column in existing table with unique values?
Drummin replied to we5inelgr's topic in PHP Coding Help
A little crude running this in a query loop but I assume this is a one time table update. <?php $sql = "SELECT id FROM users"; $result=mysql_query($sql) or die (mysql_error()); while($row=mysql_fetch_array($result)){ $user_id=$row['id']; //generate a 12 digit random alpha-numeric for session id $id=""; $length=12; $pool=""; // set pool of possible char $pool .= "abcdefghijklmnopqrstuvwxyz"; $pool .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $pool .= "0123456789"; for ($index = 0; $index < $length; $index++) { $id .= substr($pool, (mt_rand()%(strlen($pool))), 1); }//** END nested for loop for $index **// $sql = mysql_query ("Update Users Set Unique_ID = '$id' WHERE id = $user_id") or exit (mysql_error()); } ?> -
The table you had was probably fine for the comment table with for example id as the AUTO-INCREMENT field. You'll want to have that id field to identify the comment for updates etc. You just need to add the post_id field as noted and add this id when comment is added.
-
I wanted to also point you to this post regarding DB structure. This is an important concept to understand when starting out. http://forums.phpfreaks.com/index.php?topic=361424.msg1710272#msg1710272
-
You should move away from using a name to tie these records together. I assume that id is a primary key in the Dist table. In all other tables that would tie with this table, there should be a column that holds this id like dist_id. $sql="SELECT d.name, d.phone, p.pame, p.prescription, p.id FROM `dist` AS d LEFT JOIN `patientacct` AS p ON d.id = p.dist_id GROUP BY d.name"; This should pull up each district and then list any patient account data that has the dist_id, then the next record etc.
-
Have you tried LEFT JOIN?
-
Maybe I'm wrong but shouldn't the query be $query = $connection->query("SELECT group_id FROM group_membership WHERE user_id =$id");
-
<select name="event"> <?php $select_event = mysql_query("SELECT id, event FROM events ORDER by id ASC"); while($the=mysql_fetch_object($select_event)){ echo "<option value=\"" .$the->id . "\">" .$the->event . "</option>\r"; } ?> </select>
-
VERY ROUGH example Debbie based on no example to start with of what you're doing. <?php //Get current time $current_time=strtotime('now'); //Assuming member id is session $memberID=$_SESSION['memberID']; // Build query. Change table/fields to what is needed. $q1 = "SELECT a.article_title, a.comment, a.commentID, a.time, a.member_id, u.member_name FROM article_comments AS a LEFT JOIN users AS u ON a.member_id=u.member_id"; // Prepare statement. $stmt1 = mysqli_prepare($dbc, $q1); // Bind variables to query. mysqli_stmt_bind_param($stmt1); // Execute query. mysqli_stmt_execute($stmt1); // Store results. mysqli_stmt_store_result($stmt1); // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt1) > 0){ // comments Found. // Bind result-set to variable. mysqli_stmt_bind_result($stmt1, $article_title, $comment, $commentID, $time, $member_id, $member_name); //Format output to your configuration for display. Store to variable. $comment_display=""; //Loop through query results and format output while (mysqli_stmt_fetch($stmt1)){ $display_time = date("m/d/Y h:i:s A",$time); $cutoff_time = $time+(60*5); $comment_display .="<tr> <td rowspan=\"2\" align=\"center\"><strong>$member_name</strong></td> <td>$article_title<br />$display_time"; //check if member viewing is author $comment_display .=(isset($memberID) && $memberID==$member_id && $current_time<$cutoff_time ? "<style=\"float:right\"><a href=\"editcomment.php?$commentID\">Edit Comment</a></style>" : '' ); $comment_display .="</td></tr>"; $comment_display .="<tr> <td>$comment</td></tr>"; } } ?> <html> <body> <table> <?php if (isset($comment_display)){ echo "$comment_display";} ?> </table> </body> </html>
-
Any chance of uploading a sql file of your products table?
-
You sorted this out already in another thread, right? COUNT('id')
-
Hey my bad for adding the or die() statement in my example. I still think the COUNT(`id`) should be straight single quotes, i.e. COUNT('id') My thought is that DB connection is not available within the function, but as I normally don't do queries in functions I can't say for sure. Again sorry if I mislead you.
-
Just as an example, this works for me when I define $user as a name from a test DB. <?php $user="Bunny"; function dst_members($username){ return (mysql_result(mysql_query("SELECT COUNT('id') From user WHERE username ='$username'"), 0) == 1) ? true : false OR DIE ("Error: ".mysql_error()); } echo dst_members($user); ?>
-
Maybe try ('id') instead of (`id`) return (mysql_result(mysql_query("SELECT COUNT('id') From user WHERE username ='$username'"), 0) == 1) ? true : false;