Getting value with array - php

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>

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');

Retrieving Wikipedia Data

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.

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>

Sort Foreach Loop after ID

Is it possible to sort the result of a foreach loop after the ID? With the highest ID first and so on... Since it's articles I want the highest ID (newest) to be first :)
My code:
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>CMS</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
CMS
<ol>
<?php foreach ($articles as $article) { ?>
<li>
<a href="article.php?id=<?php echo $article['article_id']; ?>">
<?php echo $article['article_title']; ?>
</a>
- <small>
Posted <?php echo date('l jS', $article['article_timestamp']); ?>
</small>
</li>
<?php } ?>
</ol>
<br />
<small>Admin Login</small>
</div>
</body>
</html>
You're looking for sorting the array $articles according to article_id field - which can be done using the function usort.
Example:
<?php
function cmp($a, $b)
{
if ($a['article_id'] == $b['article_id']) {
return 0;
}
return ($a['article_id'] < $b['article_id']) ? -1 : 1;
}
usort($articles, "cmp");
foreach ($articles as $article) {
echo "$article: ". $article['article_id']." \n";
}

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