Retrieving Wikipedia Data - php

Hi could someone point out and explain where I am going wrong in trying to retrieve data from Wikipedia based on a user's search? Please see the code below. Thanks.
<html>
<head></head>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" />
</form>
<?php
// if form submitted
if (isset($_POST['q'])) {
$search = $_POST['q'];
$url = "http://en.wikipedia.org/w/api.php?
action=query&list=search&srwhat=text&format=json&srsearch={$search}&continue=";
$res = file_get_contents($url);
$data = json_decode($res);
?>
<h2>Search results for '<?php echo $_POST['q']; ?>'</h2>
<ol>
<?php foreach ($data->query->search as $r): ?>
<li><a href="http://www.wikipedia.org/wiki/
<?php echo $r['title']; ?>">
<?php echo $r['title']; ?></a> <br/>
<small><?php echo $r['snippet']; ?></small></li>
<?php endforeach; ?>
</ol>
<?php
}
?>
</body>
</html>

Try following updated code:
<html>
<head></head>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" />
</form>
<?php
// if form submitted
if (isset($_POST['q'])) {
$search = $_POST['q'];
$url = "http://en.wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=json&srsearch=$search&continue=";
$res = file_get_contents($url);
$data = json_decode($res);
echo "<pre>";
print_r($data);
echo "</pre>";exit;
?>
<h2>Search results for '<?php echo $_POST['q']; ?>'</h2>
<ol>
<?php foreach ($data->query->search as $r): ?>
<li><a href="http://www.wikipedia.org/wiki/
<?php echo $r->title; ?>">
<?php echo $r->title; ?></a> <br/>
<small><?php echo $r->snippet; ?></small></li>
<?php endforeach; ?>
</ol>
<?php
}
?>
</body>
</html>

Please remove space before "action" in url
$url = "http://en.wikipedia.org/w/api.php?
action=query&list=search&srwhat=text&format=json&srsearch={$search}&continue=";
Should Be
$url = "http://en.wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=json&srsearch={$search}&continue=";
You can also use urlencode.

Related

why [pg_num_rows() expects parameter 1 to be resource, bool given]

I wanna pick up record and show but this error happen.
this error was not happen when I add ['] both sides of post and id in line 30.
anyway I don't no why $post['name'] and $post['content'] doesn't appear.
[table has a data by me on this program.]
please give me advice.
<?php
include'php.php';
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>board</title>
</head>
<body>
<form action="#" method="post">
<?php if(count($errors)): ?>
<ul>
<?php foreach($errors as $error): ?>
<li>
<?php echo htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<p>name</p>
<input type="text" name="name">
<p>comment</p>
<textarea type="textarea" name="content"></textarea>
<br>
<input type="submit">
</form>
<?php
$sql = "SELECT * FROM post ORDER BY id DESC";
$sth = $dsn->prepare($sql);
$result = $sth->execute();
?>
<?php if ($result !== false && pg_num_rows($result)): ?>
<ul>
<?php while ($post = pg_fetch_assoc($result)): ?>
<li>
<?php echo htmlspecialchars($post['name'], ENT_QUOTES, 'UTF-8'); ?>
<?php echo htmlspecialchars($post['contents'], ENT_QUOTES, 'UTF-8'); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</body>
</html>
php.php
$dsn = new PDO('pgsql:dbname=board host=127.0.0.1 port=5432', 'postgres', 'Bossmanbig123');

php form submitting info twice

I have a form that's supposed to enter a reply to a forum topic into the database and redirect the user back to the same topic. After much trial and error I have finally got the form to work, only it is putting two identical entries into the db every time. I cannot figure out why. I have looked up this same problem and most of the other people were not redirecting after the form submission or they were using AJAX or jquery or something. Here is my page info:
<?php
session_start();
include_once('includes/config.php');
include_once('classes/topic.php');
include_once('classes/post.php');
include('includes/header.php');
?>
<link rel="stylesheet" href="css/dd.css">
<?php
$topic = new Topic;
if (isset($_GET['id']))
{
$topic_id = $_GET['id'];
$data = $topic->fetch_data($topic_id);
if (isset($_POST['content']))
{
// someone posted a reply
$date = date('Y-m-d H:i:s');
$by = $_SESSION['user_id'];
$query = $pdo->prepare("INSERT INTO dd_posts (post_content, post_date, post_by, post_topic) VALUES (? ,? ,?, ?)");
$query->bindParam(1, $_POST['content']);
$query->bindParam(2, $date);
$query->bindParam(3, $by);
$query->bindParam(4, $_GET['id']);
$query->execute();
$result = $query->execute();
header("location:topic.php?id=".$_GET['id']);
exit;
}
?>
<div id ="wrapper">
<div class="drop-section">
<div id="menu">
<a class="item" href="drop_index.php">Dead Drop</a>
<a class="item" href="add_topic.php">New Post</a>
<a class="item" href="admin/add_cat.php">New Category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() ) {
echo 'Hello ' . $_SESSION['user_name'] . '. How are you?';
} else {
echo '<a class="item" href="login.php">Sign in</a> or <a class="item" href="index.php">Create an account</a>';
}
?>
</div>
</div>
<table>
<tr class = "header-row">
<div id = "sans">
<?php echo $data['topic_subject']; ?>
- <small>started by <?php echo $data['user_name']; ?> </small><br />
<?php echo $data['topic_content']; ?>
</div>
</tr>
<?php
// retrieve all the replies to the original topic
$post = new Post;
$topic_id = $_GET['id'];
$posts = $post->fetch_all_posts_by_topic($topic_id);
?>
<tr>
<td class="first-column">
<?php foreach ($posts as $post) { ?>
<div class="drop-content-box">
<li><?php echo $post['post_content']; ?><br />
<div class = "forum-user-info">
<a href="player.php?id=<?php echo $post['user_id']; ?>">
<?php echo $post['user_name']; ?></a> - level:
<?php echo $post['user_level']; ?>
</div>
</li>
</div>
<?php } ?>
</td>
</tr>
</table>
<?php
if( $user->is_logged_in() )
{
?>
<div id = "header-section">Reply</div>
<?php if (isset($error)) { ?>
<small><?php echo $error; ?></small>
<?php } ?>
<form action="<?php echo "topic.php?id=".$_GET['id']?>" method="post" autocomplete="off">
<small><i>Do not post the actual answer to any level.</i></small><br />
<textarea rows="15" cols="50" name="content" placeholder="Give us your thoughts..."></textarea><br />
<input type="submit" value="Post" />
</form>
</div>
</div>
<?php
} else {
echo '<div id = "errors"><small>You must be signed in to reply.</div></small>';
}
}
include_once('includes/footer.php');
?>
You're executing the query twice.
$query->execute();
$result = $query->execute();

Getting value with array

I am struggling to get the value of an element in a particular array. I would like to get the value of the "logo" in this case to return "Logo Google 2013 Official.svg" from the code below.
Any help is greatly appreciated.
<html>
<head>
</head>
<body>
<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="google" />
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$url_2 =
"http://en.wikipedia.org/w/api.php?
action=query&prop=revisions&rvprop=content&format=json&titles=$search&rvsection=0&continue=";
$res_2 = file_get_contents($url_2);
$data_2 = json_decode($res_2);
?>
<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>
<?php foreach ($data_2->query->pages as $r):
?>
<li>
<?php foreach($r->revisions[0] as $a);
echo $a; ?>
</li>
<?php endforeach; ?>
</ol>
<?php
}
?>
</body>
</html>
The resulting $url_2 is http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=google&rvsection=0&continue=
Use a regular expression to capture what you want:
<ol>
<?php foreach ($data_2->query->pages as $r): ?>
<?php foreach($r->revisions[0] as $a): ?>
<li>
<?php
preg_match_all('/ logo += +([^|]+)/', $a, $result, PREG_PATTERN_ORDER);
echo trim($result[0][0]); // prints 'Logo Google 2013 Official.svg'
?>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ol>

How Can I Add Value to Save / Apply Changes with JToolBarHelper

I have an edit view which need to pass a hidden value through the form in a Joomla 2.5 component. Where would I go about adding this form value?
Here is my attempt (that is not working):
In the ../admin/models/forms/componentview.xml file I have added the following field:
<field
name="variableToPassBackToMainView"
type="hidden"
value="1"
/>
My thought was that by adding this hidden field to the model form, when it was saved then the variableToPassBackToMainView would be picked up by the new view. The view has the following variable definition to retrieve the value:
$variableToPassBackToMainView = $_REQUEST["variableToPassBackToMainView"];
Updated Info:
Joomla 2.5 Component
Here is the edit.php
<?php
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
$params = $this->form->getFieldsets('params');
?>
<form action="<?php echo JRoute::_('index.php?option=com_mycomponent&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="mycomponent-form" class="form-validate">
<div class="width-60 fltlft">
<fieldset class="adminform">
<legend><?php echo JText::_( 'COM_MYCOMPONENT_DETAILS' ); ?></legend>
<ul class="adminformlist">
<?php foreach($this->form->getFieldset('componenttitledetails') as $field): ?>
<li><?php echo $field->label;echo $field->input;?></li>
<?php endforeach; ?>
</ul>
</div>
<div class="width-40 fltrt">
<?php echo JHtml::_('sliders.start', 'mycomponent-slider'); ?>
<?php foreach ($params as $name => $fieldset): ?>
<?php echo JHtml::_('sliders.panel', JText::_($fieldset->label), $name.'-params');?>
<?php if (isset($fieldset->description) && trim($fieldset->description)): ?>
<p class="tip"><?php echo $this->escape(JText::_($fieldset->description));?></p>
<?php endif;?>
<fieldset class="panelform" >
<ul class="adminformlist">
<?php foreach ($this->form->getFieldset($name) as $field) : ?>
<li><?php echo $field->label; ?><?php echo $field->input; ?></li>
<?php endforeach; ?>
</ul>
</fieldset>
<?php endforeach; ?>
<?php echo JHtml::_('sliders.end'); ?>
</div>
<div>
<input type="hidden" name="task" value="componentview.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>

php table with hyperlink - record issue

I have two tables listed (on screen) in PHP and the left should be hyperlinked so when click on it the right table will show a query. So at the beginning it should be empty then when clicked refresh the page with the selected listname's result.
unfortunately I have no experience with these things so i don't know the concept of it yet, but I am happy to learn:)
<form method="post" action="test.php">
<div id="left"><table>
<?php
$left="SELECT * FROM groups";
$resultleft=mysql_query($left);
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
?>
</table></div>
<div id="right"><table>
<?php
$right="SELECT * FROM grouplink WHERE grouplink.group_id= ";
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
}
?>
</table></div>
<?php
if (isset($_POST('???'))){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
?>
</form>
any help would be appreciated
Can for example link to table.php?gid=n, where n would be the group id. You can then check if $_GET['gid'] isset, and if it is, take that id and put it in your query.
if(isset($_GET['gid']))
$right = sprintf("SELECT * FROM grouplink WHERE grouplink.group_id=%u", $_GET['gid']);
You need mysql_fetch_assoc
instead of mysql_query
in:
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and also in:
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
so this will be:
while ($left=mysql_fetch_assoc($resultleft)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and similar issue with right
Thanks to Svish here is my new working code:
<?php include("db_con1.php");?>
<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<div id="left">
<?php
$queryl = $pdo->prepare('SELECT id, name FROM test1 ORDER BY name ASC');
$queryl->execute();
?>
<ul>
<?php foreach ($queryl as $i => $rowl) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_add[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowl['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowl['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
<div id="right">
<?php
if(isset($_GET['gid'])) {
$gid=$_GET['gid'];
$queryr = $pdo->prepare('SELECT test3.name FROM test1, test2, test3 WHERE test1.id=test2.groupid AND test3.id=test2.peopleid AND test1.id='.$gid.' ORDER BY test3.name ASC');
$queryr->execute();
}
?>
<ul>
<?php foreach ($queryr as $i => $rowr) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_del[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowr['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowr['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
</form>
</body>
</html>

Categories