I've got some problems with my url on my website. I'm trying to get a link with the given GET parameters, but i'm getting my previous parameter aswell.
My url looks like this:
www.cdwinkel.dev/search-results?genre=Pop&medium=DVD&medium=Single .
It should be:
www.cdwinkel.dev/search-results?genre=Pop&medium=Single .
I'm running the following code:
$data['url'] = createurl();
function createurl(){
$i = 1;
$string = "?";
$keys = array_keys($_GET);
foreach($_GET as $get){
if($get != ""){
$string .= $keys[$i] . "=" . $get ."&";
$i++;
}
}
$string = rtrim($string, "&");
return $string;
}
$i = 1, because my first value in my array is empty.
And my button looks like this:
<a href='".$data['url'].'&medium='.$names[$i]."'>
I guess I should'nt set &medium=.$names[$i] in the href tag,
but I wont get the new $names[$i] in my function, so I won't get a new url if i wont add it in.
I'm looking forward to your responce.
Sincerely,
Kars Takens
At this point i've created an array with the right arraykeys and values.
$url = array_slice($_GET, 1);
This returns the following array:
array (size=2)
'genre' => string 'Pop' (length=3)
'medium' => string 'DVD' (length=3)
After this I decoded this into a new string:
genre=Pop&medium=DVD
I got 6 button which I created in a foreach loop, but i'm getting &medium='VALUE' Twice. This only happens after the first time. So the first time my button works well.
<?php
$names = array_keys($data['tellen']);
$i = 0;
foreach($data['tellen'] as $m){
echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
$i++;
}
Hopefully you can help me further with this information.
I solved my problem by adding this in my forloop:
foreach($data['tellen'] as $m){
if(isset($_GET['medium'])){
unset($_GET['medium']);
$url = array_slice($_GET, 1);
$data['url'] = urldecode(http_build_query($url));
}
echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
$i++;
}
I get the following error Fatal error: Call to a member function execute() on a non-object in /home/wt/public_html/view-reports.php on line 237 .
I also see another problem. The variables generated doesn't echo the right corresponding data. For instance, $rep_type echo position. Could you please help me on this. Thank you! Test site url is here. ethioserver.com/~wt/view-reports.php?rep_id=144 The problem is not visible in wampserver. The php version in both servers is 5.4.
The main code is below.
<?php
//generate page
$_GET['rep_id']>0;
if ($_GET['rep_id']!=0){
require ('includes/db.php');
mysqli_select_db($con, $db_name);
$sql= 'SELECT * FROM Reports';
$stmt = $con->prepare($sql);
$stmt->execute();
$stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $rep_type, $department, $position, $report, $rep_to);
$stmt->fetch();
//allow users to edit
if($fname . ' '. $lname!=$rep_by){
echo '<div class="links"></div>';
}
else {
echo '<div class="links">';
echo '<a href="edit-this-report.php?rep_id=' . $rep_id;
echo 'target="_blank" img src="images/pdf.png" alt="edit report" target="_blank" ><img src="images/edit.png"> </a>';
echo '</div>';
}
echo '<div class="links"><a href="pdf/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.pdf"
target="_blank" img src="images/pdf.png" alt="download report"><img src="images/pdf.png"> </a></div>';
//start html creation
ob_start();
echo "<h1>$rep_by ($rep_date)</h1>";
echo '<div class="infobar"><strong>Report Type: </strong>'. $position . '</div>' ;
echo '<div class="infobar"><strong>Department: </strong>'. $rep_type . '</div>';
echo '<div class="infobar"><strong>Position: </strong>'. $department . '</div>';
echo $report;
file_put_contents(('scripts/dompdf/html/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.html'), ob_get_contents());
if ($rep_ledit_date>0) {echo '<div class="infobar">' . 'Last Edited: ' . $rep_ledit_date . '</div>';
} else {echo "";
//end html creation
ob_end_flush();}
//generate pdf using dompdf
require_once "scripts/dompdf/dompdf_config.inc.php";
$file='scripts/dompdf/html/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))) . '.html';
$dompdf = new DOMPDF();
$dompdf->load_html_file($file);
$dompdf->render();
$canvas = $dompdf->get_canvas();
//For the header
$header = $canvas->open_object();
$font = Font_Metrics::get_font("helvetica", "bold");
$date = date("Y-m-d H:i:s");
$canvas->page_text(500, 20, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 8, array(0, 0, 0));
$canvas->page_text(30, 20, "Crystal Reporting System", $font, 8, array(0, 0, 0));
$canvas->page_text(300, 20, $date, $font, 8, array(0, 0, 0));
$canvas->close_object();
$canvas->add_object($header, "all");
//For Footer
$footer = $canvas->open_object();
$font = Font_Metrics::get_font("helvetica", "bold");
$canvas->page_text(30,750, "$rep_by $position", $font, 8, array(0, 0, 0));
$canvas->page_text(440,750, "crystalreportingsystem.com", $font, 8, array(0, 0, 0));
$canvas->close_object();
$canvas->add_object($footer, "all");
$output = $dompdf->output();
file_put_contents('pdf/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.pdf', $output);
}
else {
?>
<h1>View Latest Reports</h1>
<p> You can now view all reports by clicking on the links on the side menu. </p>
<?php
}
?>
The problem area.
$_GET['rep_id']>0;
if ($_GET['rep_id']!=0){
require ('includes/db.php');
mysqli_select_db($con, $db_name);
$sql= 'SELECT * FROM Reports';
$stmt = $con->prepare($sql);
$stmt->execute();
$stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $rep_type, $department, $position, $report, $rep_to);
$stmt->fetch();
The line with error. is
$stmt->execute();
Thanks!
I expect some error checking ought to shed light on the problem. Add or die(mysqli_error($con)) after the mysqli_select_db and $con->prepare functions:
require ('includes/db.php');
mysqli_select_db($con, $db_name) or die(mysqli_error($con));
$sql= 'SELECT * FROM Reports';
$stmt = $con->prepare($sql) or die(mysqli_error($con));
$stmt->execute();
Regarding the mismatched variables, apparently the order of table columns is not corresponding with the order of the variable assignments. I'm guessing at your table names but I expect this gives you the idea. Change or $sql assignment to:
$sql= 'SELECT rep_id, rep_date, rep_ledit_date, rep_by, rep_type, department, position, report, rep_to FROM Reports';
Alternatively you could reorder the variable assignments to match the order of the columns in the database table Reports, however the way I showed you above will not break if someone where to add or reorder table columns in the future.
On a side note, what is the purpose of this line at the top:
$_GET['rep_id']>0;
I don't see an assignment, or conditional evaluation. Looks like it's doing nothing.
Hi I am trying to add child category selection list and I found one nice code here and works fine but I want to use it as a dropdown selectoin and not un order list. Can anyone help me to do that.
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<h3>Products</h3>';
echo '<ul>'.$this_category.'</ul>';
}
}
?>
I have tried to use foreach but it didnt work may be I am wrong somewhere as not master in php
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<h3>Products</h3>';
echo '<select>';
foreach($this_category as $list) {
echo '<option>'.$list.'</option>';
}
echo '</select>';
}
}
?>
So your code doesn't appear to enter the foreach loop. The variable $this_category seems to be a string, not an array that you can iterate over.
Do a string replace to switch the <li> tags with <option> and then echo it:
$this_category = str_replace('<li>', '<option>', $this_category);
$this_category = str_replace('</li>', '</option>', $this_category);
echo '<select>'.$this_category.'</select>';
i have been trying to implement jquery ui autocomplete feature.. and the basic html code is xactly the same as given on-
http://jqueryui.com/demos/autocomplete/#remote
I have tried all combinations/ code snippets for "search.php" such as-
1)
<?php
if ( !isset($_REQUEST['term']) )
exit;
// connect to the database server and select the appropriate database for use
$dblink = mysql_connect('localhost', 'root', '') or die( mysql_error() );
mysql_select_db('research');
$rs = mysql_query('select uname,email,password from login where uname like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by uname asc limit 0,10', $dblink);
// loop through each zipcode returned and format the response for jQuery
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
$data[] = array(
'label' => $row['uname'] .', '. $row['password'] .' '.$row['uname'] ,
'value' => $row['uname']
);
}
}
// jQuery wants JSON data
echo json_encode($data);
flush();
?>
2)
<?php
include("includes/connect.php");
$query = "SELECT uname from login WHERE uname LIKE '%" . addslashes($_GET['term']) . "%'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
foreach($row as $val)
$tab[] = $val;
}
print json_encode($tab);
?>
3)
<?php
include("includes/connect.php");
$term = $_REQUEST['m']; // where name of the text field is 'm'
$query = "SELECT * FROM login WHERE uname LIKE '%$term%'";
$result = $mysqli->query($query);
$arr = array();
while($obj = $result->fetch_assoc()) {
$arr[] = $obj['nome'];
}
echo json_encode($arr);
?>
But still nothing is working !! that is when i type into the text box for autocomplete there are no results shown. what could be the error ? are there any alternative solutions ?? Please help !!!
The variable that you pass into json_encode must be an array of strings.
e.g. ["red","green","blue"]
or an array of object literals e.g. [{"value" : "red"},{"value" : "green"},{"value" : "blue"}]
Each time through the loop you need to append the item to the existing items.
I notice, in all 3 code snippets, that you assign the current item to your array variable, instead of appending it. When the loop completes, the only item in your array will be the item processed most recently by the loop.
So unless what you type in the field matches that one item, the autocomplete isn't going to show anything.
Here is my PHP code to build an array of strings before sending it back to the file that made the request.
$fetchedArtists = $db->query($queryToGetArtists );
$json = '[';
$first = true;
while($row = $fetchedArtists->fetch_assoc())
{
if (!$first)
{
$json .= ',';
}
else
{
$first = false;
}
$artist = $row['artistName'];
$json .= '{"value":"'.$artist.'"}';
}
$json .= ']';
echo $json;
I am trying to run some code from a book. There appears to be a problem with the code.
Here is the error message:
Fatal error: Can't use function return
value in write context in
/Applications/MAMP/htdocs/Eclipse-Workspace/simpleblog/test.php
on line 24
Here is the code referenced in the message (starting on line 24)
if (!empty(trim($_POST['username']))
&& !empty(trim($_POST['email']))) {
// Store escaped $_POST values in variables
$uname = htmlentities($_POST['username']);
$email = htmlentities($_POST['email']);
$_SESSION['username'] = $uname;
echo "Thanks for registering! <br />",
"Username: $uname <br />",
"Email: $email <br />";
}
I would appreciate any help. Please let me know if I need to provide any more information
Thanks a lot guys. That was very fast. The solution works great.
The problem is that the empty() function needs to be applied only to direct variables.
For future reference:
The code is from 'PHP for Absolute Beginners' by Jason Lengstorf (2009), pages 90-91, Chapter 3, $_SESSION
corrected code:
//new - Created a variable that can be passed to the empty() function
$trimusername = trim($_POST['username']);
//modified - applying the empty function correctly to the new variable
if (!empty($trimusername)
&& !empty($trimusername)) {
// Store escaped $_POST values in variables
$uname = htmlentities($_POST['username']);
$email = htmlentities($_POST['email']);
$_SESSION['username'] = $uname;
echo "Thanks for registering! <br />",
"Username: $uname <br />",
"Email: $email <br />";
}
In short: The empty() function only works directly on variables
<?php
empty($foo); // ok
empty(trim($foo)); // not ok
i'd say, for the course of getting further with that book, just use a temporary variable
so change:
if (!empty(trim($_POST['username']))
to
$username = trim($_POST['username']);
if(!empty($username)) {
//....
Exactly your example is mentioned at the manual
Note:
empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
Use a temporary variable, or just test against "empty string"
if (trim($foo) !== '') {
// Your code
}