Hi i have a sorting question to ask.
My database has images with "Photoname" as one of the columns in mySQL table.
Database:
Photoname
Apple
Bear
Cat
Orange
So in my catalog.php, I would display all the images from the database. Then, i have a navigation bar(div) to allow viewers to filter the images. So users can view by A | B | C... | Z|.
My question is with regards to the URL and approarch. I would create imagesort.php which would handle all the mySQL in a general way( so that i don't have to waste time creating for A - Z).
Should the imagesort.php be something like imagesort.php?sort=A? Then in imagesort.php how can i get the value A? For example:
select photoname, date from image where photoname LIKE 'a%'
And also if the above way is correct, how can i parse this variable A to the link in catalog.php?
Here is what i've done so far in my catalog.php:
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
...
</ul>
Do i really have to do this for 26 entries? or is there a simpler method?
You can use something like this...
<ul>
<?php for($i=65;$i<90;$i++) { ?>
<li>&#<php echo $i;?>;</li>
<?php } ?>
</ul>
You'll need to get the value of sort from the $_GET array. Make sure you sanitize your input before using it in your SQL query.
$match = "";
if(isset($_GET['sort']))
{
// Escape the string to guard against SQL injection
$match = mysql_real_escape_string($_GET['sort']);
}
$query = "select photoname, date from image where photoname LIKE '$match%'";
This code has the effect of working for any value of sort, including an empty string. If you want to limit the valid strings to a single uppercase alpha character, you'll have to check separately for that, like so:
if(strlen($match) != 1 || ctype_upper($match) == false)
{
$match = "";
// Maybe some other error condition
}
If the links are formatted like that then they will end up in the $_GET super global:
$sort = $_GET['sort'];
For the second question, you can easily use a loop in php to run through the letters of the alphabet, to generate the pagination content you need:
$link_base = 'imagesort.php';
$pagination_content = '<ul>';
for($i=65; $i<=90; $i++)
{
$pagination_content .= '<li>'.chr($i).'</li>';
}
Related
I have a dynamic menu and I want to send the user to a new page on click with some db values. I want to use a session here without forms or via $_GET.
Presently, using the code below, only the last value of the loop is appearing in the $_SESSION. Perhaps I need to use a 2D array here, but I am not sure what to do:
<?php
$i = $cit['city_id'];
$selectCity = $dbh->prepare("SELECT * FROM `table` where city_id=$i");
$selectCity->execute();
$cityNum = $selectCity->fetchAll();
foreach($cityNum as $cities)
{
$_SESSION['$centreNum'] = $cities['centre_id'];
?>
<li class="first"><?php echo ucwords($cities['centre_location']); echo $_SESSION['$centerNum']; ?></li>
<?php
}
$centre_num = $cities['centre_id'];
?>
You have a couple problems (the one I think you mean to do...) in your example:
...
$cityNum = $selectCity->fetchAll();
foreach($cityNum as $cities)
{
# Not sure if you are trying keep this a variable key or not.
# | Make this session an array, you keep overwriting variable
# | |
# | |
# v------+---v v
$_SESSION['$centreNum'][] = $cities['centre_id'];
?>
...continue
i'm very newbie trying to finish a job, so let me explain the issue here:
i have a chasback website, there are retailers on database, my links are :
a href="/view_retailer.php?rid=<?php echo $tops_row['retailer_id']; ?>"
The php page that show the content have:
if (isset($_GET['rid']) && is_numeric($_GET['rid']))
{
$retailer_id = (int)$_GET['rid'];
}
else
{
header ("Location: index.php");
exit();
}
That display on my URL: /view_retailer?rid=8
I want to change that to /view_retailer?rid=retailer-title
I can't figure why its not working, i have on my DB the columns retailer_id and title.
Then i need to change from /view_retailer?rid=retailer-title to /loja/retailer-title through mod_rewrite.
How can i reach there? thanks for helping me!
I can answer the first bit
a href="/view_retailer.php?rid=<?php echo $tops_row['retailer_id']; ?>"
Needs to be:
<a href="/view_retailer.php?rtitle=<?php echo $tops_row['retailer_title']; ?>"
And
if (isset($_GET['rtitle']) && is_string($_GET['rtitle']))
{
$retailer_title = (string)$_GET['rtitle'];
}
else
{
header ("Location: index.php");
exit();
}
You should also sanitise anything coming from GET/POST to make sure it is safe if you intent to query the database with it.
As for the mod_rewrite. In very rubbish at that so I would not be able to help with that.
How about using 3 tables in your DB?
retailer_id retailer_name and retailer_url_name
values (example)
43 The Awesome Supermarket and awesome-supermarket
Check $_GET['rid'] with a regex pattern to check for letters and the - character and match against retailer_url_name in the DB?
// check for letters and -
if (preg_match('/^[a-z\-]+$/', $_GET['rid']) {
// do stuff here
} else {
// invalid ID
}
In the retailers table add column named "slug" or something like that. Then you have to modify you query thus you are not looking for id but certain string (slug). Make sure that your slugs contains only signs proper for web addresses: no spaces, non-latin letters etc.
When it comes to code, it may look something like that:
if (!empty($_GET['title']))
{
$title = someSanitizeFunc($_GET['title']);
$retailer = db()->queryOne("SELECT id FROM retailers where slug = '".$title."');
}
It's just example, you have to adjust it to your cms or whatever you use.
Please help me ...
I'm a newbie! Please tell me what to do.
processed.php
<?php
include_once('../../dbConnect.inc.php');
$sql="SELECT name FROM artist";
$artist = select($sql);
disconnect();
$output_items = array();
while($row = mysql_fetch_array($artist))
{
$results[] = array('label' => $row['name']);
}
echo json_encode($results);
?>
index.php
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#artist").autocomplete(
{
source: 'processed.php',
});
});
</script>
I have this problem: http://jsbin.com/alaci5
Autocomplete expects the source (when an URL is specified to filter out the results).
From documentation:
String: When a string is used, the Autocomplete plugin expects that
string to point to a URL resource that will return JSON data. It can
be on the same host or on a different one (must provide JSONP). The
Autocomplete plugin does not filter the results, instead a query
string is added with a term field, which the server-side script should
use for filtering the results. For example, if the source option is
set to "http://example.com" and the user types foo, a GET request
would be made to http://example.com?term=foo. The data itself can be
in the same format as the local data described above.
So in your PHP code you have to do:
include_once('../../dbConnect.inc.php');
$sql="SELECT name FROM artist WHERE `name` like '%".mysql_real_escape_string($_GET['term'])."%'";
$artist = select($sql);
$output_items = array();
while($row = mysql_fetch_array($artist)) {
$results[] = array('label' => $row['name']);
}
echo json_encode($results);
That autocomplete function is probably passing a few variables to your processed.php page.
Use var_dump($_GET) to see all the things you're being passed.
Inside one of those $_GET elements, you'll have the contents of the text box as they exist on the page. For the sake of demonstration, I'm going to use $_GET['text']. You'll need to find out which part holds the data you need.
What you'll need to do is search the database using this value for a list of results to return.
$sql="SELECT name FROM artist";
$artist = select($sql);
This is your script as it exists. What it may end up looking similar to is this.
$text_input = mysql_escape_string($_GET['text']);
$sql="SELECT name FROM artists WHERE name LIKE '%$text_input%'";
$artist = select($sql);
You'll want to get results that are similar to the inputted text on the user-facing page.
A few notes for you
I used mysql_escape_string() solely to may what you already have. While this does work (driving around a busted-ass chevy pacer works too, but there are much better ways though), its not recommended, which sets us up for point 2.
Using the old mysql extension is not really a good idea, its been replaced by mysqli, and PDO.
you'll need to escape your data, this is how its done with mysqli.
I'm working out a social tool like twitter, but I got stuck on "who also tweet this twitter."
There's a list of twitters on my page, and I wanna display who also tweeted this for each tweet.
I'm using MySQL + php + Smarty.
Here's the code:
/* ---- php ---- */
// Get twitters that tweeted over 200 times.
$sql = mysql_query("SELECT id, content, user, tweets FROM twitter_list WHERE tweets > '200'");
$twitter_array = array();
while($tweet_row = mysql_fetch_array($sql)){
array_push($twitter_array, $tweet_row);
// Get the users who tweeted these twitters.
$twitter_id = $tweet_row['id'];
// Find out 5 friends who also tweeted this twitter. twitter_relation stores who tweets what.
$friends_who_also_tweet = mysql_query("SELECT tid, twitter_id, user_id FROM tweet_relation WHERE twitter_id = '$twitter_id' ORDER BY tid DESC LIMIT 5");
$friends_who_also_tweet_array = array();
while($friends_who_also_tweet_row = mysql_fetch_array($friends_also_tweet_array)){
array_push($friends_who_also_tweet_array, $friends_who_also_tweet_row);
}
if($friends_who_also_tweet_array){
$sm->assign("fwat", $friends_who_also_tweet_array);
}
}
if($twitter_array){
$sm->assign("twitter", $twitter_array);
}
$smarty->display('twt.html');
/* ---- HTML: twt.html ---- */
...
{section name=twitter loop=$twitter}
<div class="content">{$twitter[twitter].content}</div>
<div class="who_also_tweet">
{section name=who loop=$fwat}
<div class="i_also_tweet">{$fwat[who].user_id}</div>
{/section}
</div>
{/section}
I wish to get the array of twitter, and for each twitter, retrieve the 5 users who tweets it. But it only display the first twitter's users, I suspect while() might be wrong but cannot find it out. Anyone could lend me a hand? Many thanks.
$twitter_array = array();
then:
if($twitter_array){
$sm->assign("twitter", $twitter_array);
}
You never added anything to that array. I'm assuming you mean to do it here (since $tweet_array is never declared):
array_push($tweet_array, $tweet_row);
In fact, this variable also gets assigned and then never referred to as well:
$friends_who_also_tweet
Your variable naming is really confusing, and I think it may be contributing to your problems.
Edit: Also, why do you assign this with every iteration of the first While loop? It is just going to overwrite the previous values:
if($friends_who_also_tweet_array){
$sm->assign("fwat", $friends_who_also_tweet_array);
}
I have been working on this one topic for weeks. I'm creating a webpage that pulls information from MsSQL server. My problem is where there is a section with few check boxes.
The checkboxes are suppose to be checked if they are found in the SQL database.
If the borrower used money from "Savings", "Checking" and "Stock" those checkboxes should be checked in HTML page. In my case it is only checking whatever is on the first row of the SQL search list. So if the list has "Saving" on the first row, only the "Saving" checkbox will be checked not the rest on the list. I tried using loop (while($r->EOF)), but then it picks whatever is on the end of the list. Here is what I am using to pull data from the SQL server. Thank you in advance for your help. Really appreciate it!
function __construct($_ldid, $_lrid)
$this->ldid = $_ldid;
$this->lrid = $_lrid;
//$loan is defined in the PHP.class (which is shown below).
$q = ("SELECT * FROM Tabel_name where loan_id = 885775")
$v = array($_ldid, $_lrid);
$r = $db->Execute($q, $v);
$this->downpaymenttype = $r->fields; //<- I think I have this line done wrong because it is suppose to store the outputs to an array.
//So wherever the "$loan" is in HTML page, it will take the outputs from the above statement.
//The above code, which is in PHP.class file, outputs the following(or suppose to):
1 885775 Checking
2 885775 Saving
3 885775 Gift
In the HTML webpage, the following codes should check mark the boxes according to the outputs:
<input type="checkbox" name="DPS1[]" id="DPS1-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Checking"} checked {/if}/>
<input type="checkbox" name="DPS2[]" id="DPS2-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Saving"} checked {/if}/>
<input type="checkbox" name="DPS3[]" id="DPS3-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Gift"} checked {/if}/>
Only the "checking" checkbox is getting checked because that's the one on the first row.
I also tried to loop, but it is not storing in array (I guess I don't know how to use the array for session)
$q = ("SELECT Tabel_name FROM loan_downpaymentsource where loan_id = 885775");
$v = array($_ldid, $_lrid));
$r = $db->Execute($q, $v);
while(!$r->EOF) {
$this->downpaymenttype = $r->fields;
$r->MoveNext();
}
Add a loop to go through your results before displaying and set flags. $r->fields is only going to grab the fields for the current row. You need to go through your entire rowset to see if the others are set.
Something like (It looks like you're using adodb or something like that)
$checking = false;
$saving = false;
$gift = false;
while(!$r->EOF){
$row = $r->fields;
switch($row->downpaymenttype){
case 'Checking':
$checking=true;
break;
case 'Saving':
$saving=true;
break;
case 'Gift':
$gift=true;
break;
default:
break;
}
$r->moveNext();
}
Now on your view check the flags. I've not used smarty but you'll have to pass those flags to your template as well as I'm sure you know, and then change your checkbox lines to check if each flag is true/false.
Hope that helps!