extracting links as clickable links from sql table - php

Been at this for a while and cant figure it out so i thought id come to the ppl who know.
my php / sql is so-so but.....this is what im trying to do / figure out..
i have a database setup to take in messages (of any sort) and works fine. The user enters data in a field, hits send, and the message gets logged.
But when a user puts in a link for example " http://www.google.com "
its stored in the DB just fine but when its printed back onto the page, it comes back as plain
text, what i want is that when the page throws back the message with the link in it, that the link is live and clickable.
Ive googled my a$# off but im guessing im searching for the wrong thing (or im missing something unbeknownst to me. )
Any tips/*direction* etc, ill gladly accept. also, I dont mind doing the work/research so if you have links only ill take those too.
Thanks in advanced.

You need to parse messages plain text in order to find links. Then, change link texts by adding an anchor tag (<a>).
This link should help you.

you need regular expressions to detect links and convert them to ...
see the answer here:
PHP validation/regex for URL

<?php echo "your_field"; ?>

Something like this should work (I took this from some code I wrote for a client):
$query = mysql_query("SELECT * FROM wcordersinfo WHERE dealer_id = '" . $dealer_id . "' && drawing_num = " . $_GET['reference_id'] . "");
while ($orders = mysql_fetch_array($query)) {
if ($orders['drawing_num'] != '') {
$link_open = '<a href="http://www.example.com/dealers/order-details.php?reference_id=' . $orders['drawing_num'] . '">';
$link_close = '</a>';
} else {
$link_open = $link_close = '';
}
and then where you want to display the content:
<?php echo "<li>' . $link_start . $orders['carrier'] . $link_end . '</li>"; ?>

You may want to check out http://www.php.net/manual/en/function.preg-replace.php for replacing regular expressions with something.
There you want to replace every website with a typical html link syntax like ...

Related

display the same image in a loop while retrieving data from mysql table

I'm looking for a solution to the following.
I have certain data stored in a mysql table. As for now I am able to retrieve the data and display it on a page. But now I would like to add a small png to one specific part of the data. One variable is a time period, like '25-28 of March', this continually changes. But I would like to add a calendar icon in front of this. How can I best proceed. I've been looking at this for hours now.
This is my php code to display the data on the page (write_to_site.php):
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<h3> " . $row["conferentie"] . "</h3><p>" . HERE THE ICON $row["datums"] . "</p><p>" . $row["locatie"] . "</p><hr>";
}
I use php include write_to_site.php on the page where I want this displayed.
It should look something like this (this is only html):
example
So the icon should always be the same, so there is no need to store it in de database.
Thanks in advance!
change this "</h3><p>" . HERE THE ICON to "</h3><p><img src=\"myIcon.png\" />" .

Need help saving the answer from a Select into Database

Hi there;
I need some help; I need to save the answer from a select into my database. I do not know where I went wrong. Here is some details regarding my code.
I have a database named 'lib'
The table is named 'games'
I need to save the following into the table; 1) Game (all the words) and 2) Type
It is for a tournament website. So that I can make it available on the LAN via XAMPP.
Here is my code.
http://pastebin.com/iP7MCMzJ
Thanks in advance!
UPDATE:
Thanks for all the help! I have found and fixed my problem! In my database the VARCHAR was limited to 50 characters. I have fixed that. Now all is good :)
Thanks once again!
Well, you're not showing any code but this is how I'd expect it to work based on your description:
Your HTML will want to look something like:
<?php echo '<option name="game" value="' . htmlentities($game) . '">' . $row['game'] . '</option>'; ?>
Notice the name attribute? Also, I changed the way the HTML was written as yours was very tough to read.
PHP
if(isset($_POST['game']) && isset($_POST['type'])){
$game = $_POST['game'];
$type = $_POST['type'];
// PLEASE CLEAN YOUR INPUT PRIOR TO INSERTING THIS DATA!
Insert into games (Game, Type) VALUES ($game, $type)
}
else{
echo( "error getting info");
}

Curl Problem, I just don't get it

SOLVED: var_dump revealed that that the issue was of & and & .
Also I would still like to know why curl fetched a page with missing divs. Would anyone be kind enough to explain ?
have been at this problem for 1.5hours now. Narrowed down to something that just doesn't make sense to me. Please help . I am fetching a page using curl.
The problem is that when the following code is used:
a lot of div tags from the navigation to the left are missing from the fetched page,
$pageToParse = "http://www.themarketgrocer.com.au{$arrayLeftBar[$i]} " ;
;
//fetch target page
$curlFetched = http_get($pageToParse, "http://www.google.com") ; //fetched using a curl function
echo $curlFetched['FILE'];
the value of $arrayLeftBar[$i] is fetched from an html page using simplehtmldom. as echoed:
"/index.php?option=com_content&view=category&layout=blog&id=37&Itemid=92"
HOWEVER if
$pageToParse = "http://www.themarketgrocer.com.au{$arrayLeftBar[$i]} " ;
is changed to : (basically manually appending the value of $arrayLeftBar[$i] )
$pageToParse = "http://www.themarketgrocer.com.au/index.php?option=com_content&view=category&layout=blog&id=37&Itemid=92" ;
then the complete page is fetched.
WHY ?? I have echoed the values and they are identical. What silly little thing am I missing?
Edit:
THis is how the $arrayLeftBar is being populated: I am pretty sure the problem is here . Coz if I manually create a simple array. Things work.
foreach ($sublevelLinks as $link)
{
$arrayLeftBar[] = $link->href ;
}
On the $arrayLeftBar[$i] value use urldecode
$query = urldecode($arrayLeftBar[$i]);
$pageToParse = "http://www.themarketgrocer.com.au{$query}" ;
SOLVED: var_dump revealed that that the issue was of & and & .
html_entity_decode($arrayLeftBar[$i]) did the necessary conversion

delete a row from my sql table

I'm still new to php and working my way around it but i'm stuck at the following piece:
code for deleting a row in my table
i have a link directing towards this piece of my script. i run through the first half just fine but when i press on submit and try to execute my delete query it won't go to my second if statement let alone get to the delete query.
$pgd is the page id
my hunch is there is problem with the action in the form i'm building after my while statement
forgive me for the wierd formatting of my msg but its 2am and very tired, i promise to format my questions in the future better! any help is appreciated
edit: ok other then the obvious mistake of missing method=post #.#;
edit:
hey everyone,
first of all, i'd like to thank everyone for their response.
i just started coding in php last weekend so forgive my messy codes. the code is still running locally and my main goal was to finish the functions and then work on securing my code.
now back to the issue, i'm sorry if i was vague about my problem. i'll try to reiterate it.
my issue isn´t selecting an item i want to delete, the issue is that it won´t get to the 2nd if statement.
Re-edit:
this time with my current code:
if($_GET['delete'] == "y")
{
//content hier verwijderen
$sqlcont1="SELECT * FROM content where id ='".$_GET['id']."'";
echo $sqlcont1;
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while($rowcont1= mysql_fetch_array($resultcont1)){
echo '<form class="niceforms" action="?pg='.$pgd.'&delete=y&remove=y&id='.$_GET['id'].'" method="post">';
echo '<h1>'.$rowcont1['Titel'].'</h1>';
echo '<p>'.$rowcont1['Content'].'</p>';
echo '<input type="submit" value="Delete article">';
echo '</form>';
}
if($_GET['remove']=="y"){
echo 'rararara';
$id=$_GET['id'];
$sqlrem="DELETE FROM content WHERE id="$id;
echo $sqlrem;
mysql_query($sqlrem);
}
}
echoing $sqlrem gives me the following now:
DELETE FROM content WHERE id=8
that being my current code, i get in to the second IF statement but now to get it to delete!
#everyone:
ok maybe thinking out loud or following my steps worked but the code works, i know its very messy and it needs fine tuning. i'd like to thank everyone for their help and feedback. i'm liking this and you'll probably see me alot more often with nubby questions and messy codes with no escapes :(
First of all, you have SQL injection vulnerability in your script. Anyone can add some string that will be attached to your query, possibly altering it in a way that can make almost anything with the data from your database.
Escape your values with one of anti-SQL-injection methods. Read more for example on php.net/manual/en/function.mysql-query.php
To the point...
Your deletion code will be executed only if you invoke URL with two params (remove and delete set to y. That means your URL should look similar to something.php?delete=y&remove=y. Maybe you just did not spot it.
Please give details about any errors that occured and tell me whether the above mentioned solution helped.
mysql_fetch_array() returns an array
your while statement acts as an if, and does not iterate thru the array returned as you think it does
you need something like
$all_rows = mysql_fetch_array($result);
foreach ($all_rows as $row) {
$sql = "delete from table where id = " . $row['id'];
}
It looks to me like you're mixing two forms together here: you're wanting to see if you went to the delete row form (the first few lines), and you're trying to present the delete row form (the while loop.) I would break these two things apart. Have a page that simply displays your forms for row deletes, and another page that processes those requests. And another page that brings you to the delete rows page.
For now, just echo all the values you're expecting to receive in $_GET[] and see if they are what you expect them to be.
You have a lot of problems in that script alone, so just to make things easier (considering you uploaded a pic), put an
echo $sqlrem;
in your second if statement, see if the query is displayed. If not, it means it doesn't even get to that part of code, if it gets displayed, copy it and run it in phpmyadmin. That should output a more coherent error message. Tell us what that is and we'll work it through.
I also noticed that your DELETE SQL query might have an issue. If your $pgd' id is a integer, you shouldn't include the ' single quote, that is for string only.
**Correction**
$sqlrem = "DELETE FROM content WHERE id = " . controw1['id'];
EDIT
Anyway, just to help out everyone, I typed out his code for easier viewing.
I think his error is $rowcont1['Tilel'] --> that might caused PHP to have an error because that column doesn't exist. I assumed, it should be `Title' causing an typo error.
if(_$GET['delete'] == "y") {
$sqlcont1 = "SELECT * FROM content where id ='" . $_GET['id'] . "'";
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while ($rowcont1 = mysql_fetch_array($resultcont1)) {
echo '<form class = "niceforms" action = "?pg=' .$pgd . '&delete=y&remove=y">';
echo '<h1>' . $rowcont1['Title'] . '<h1>'; // <-- error here
echo '<p>' . $rowcont1['Content'] . '</p>';
echo '<input type = "submit" value = "Delete article">';
echo '</form>';
}
if ($_GET['remove'] == "y"){
$sqlrem = "DELETE FROM content WHERE id = " . $rowcont1['id'];
mysql_query ($sqlrem);
}
}

Fetch database information on a new page without using new documents

I'm working on a page where I've listed some entries from a database. Although, because the width of the page is too small to fit more on it (I'm one of those people that wants it to look good on all resolutions), I'm basically only going to be able to fit one row of text on the main page.
So, I've thought of one simple idea - which is to link these database entries to a new page which would contain the information about an entry. The problem is that I actually don't know how to go about doing this. What I can't figure out is how I use the PHP code to link to a new page without using any new documents, but rather just gets information from the database onto a new page. This is probably really basic stuff, but I really can't figure this out. And my explanation was probably a bit complicated.
Here is an example of what I basically want to accomplish:
http://vgmdb.net/db/collection.php?do=browse&ltr=A&field=&perpage=30
They are not using new documents for every user, they are taking it from the database. Which is exactly what I want to do. Again, this is probably a really simple process, but I'm so new to SQL and PHP coding, so go easy on me, heh.
Thanks!
<?php
// if it is a user page requested
if ($_GET['page'] == 'user') {
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
// db call to display user WHERE id = $_GET['id']
$t = mysql_fetch_assoc( SELECT_QUERY );
echo '<h1>' . $t['title'] . '</h1>';
echo '<p>' . $t['text'] . '</p>';
} else {
echo "There isn't such a user".
}
}
// normal page logic goes here
else {
// list entries with links to them
while ($t = mysql_fetch_assoc( SELECT_QUERY )) {
echo '<a href="/index.php?page=user&id='. $t['id'] .'">';
echo $t['title'] . '</a><br />';
}
}
?>
And your links should look like: /index.php?page=user&id=56
Note: You can place your whole user page logic into a new file, like user.php, and include it from the index.php, if it turns out that it it a user page request.
Nisto, it sounds like you have some PHP output issues to contend with first. But the link you included had some code in addition to just a query that allows it to be sorted alphabetically, etc.
This could help you accomplish that task:
www.datatables.net
In a nutshell, you use PHP to dynamically build a table in proper table format. Then you apply datatables via Jquery which will automatically style, sort, filter, and order the table according to the instructions you give it. That's how they get so much data into the screen and page it without reloading the page.
Good luck.
Are you referring to creating pagination links? E.g.:
If so, then try Pagination - what it is and how to do it for a good walkthrough of how to paginate database table rows using PHP.

Categories