How to get wiki pageid? - php

I am currently using PHP to make a chatbot that can do research on wiki,and send the introduction automaitcally, here is the wiki api json(https://zh.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=search_title)
{"batchcomplete":"","query":{"redirects":[{"from":"search_title","to":"search_title"}],"pages":{"541":{"pageid":541,"ns":0,"title":"search_title","extract":"ablablablablablablablablablablabla。"}}}}
I want to get the extract part by
$data=$jsondata['query']['pages']["541"]['extract'];
but it seems like I need to have to know pageid is '541' first, is there method that I can know padeid in first??

You should look for a way to iterate over array values without knowing the keys. I'm not a PHP developer but I think a foreach will do:
foreach ($jsondata['query']['pages'] as $pageid => $pagedata) {
extract = $pagedata['extract']
...
}
There is also a formatversion=2 parameter for the query that, among other things, will make
action=query's "pages" be an array, instead of an object with page ids
as keys that can be difficult to iterate.

Related

Overwrite the original array values in PHP in nested loop

Cutting out some of the code on the innermost foreach, I'm trying to change HERE to make it so that it alters the original value. I want to pass a pointer basically and alter it. I was able to kind of do this with the &$ keyword in the foreach but (as the docs state) it results in some buggy behavior and I'm trying to do it the way they, and others on SO suggest. The problem is all the examples I find are for a single foreach, not for nested.
The following code loops properly but when I get to the HERE it doesn't actually alter the original value. Also worth mentioning that $sources could be an array of arrays (by index) or an array of key values. This looping code seems to iterate over both fine though, just not overriding the original value of $sources
fwiw, on top of the &$ I also tried:
$sources[$sourceKey][$rowKey][$cellKey] = $date->format('m/d/Y');
Which $sources[$sourceKey][$rowKey][$cellKey] returns the right value if I print it but it still doesn't overwrite the original array.
function convertDates($sources) {
foreach($sources as $sourceKey => $sourceValue){
foreach ($sourceValue as $rowKey => $rowValue) {
foreach ($rowValue as $cellKey => $cellValue) {
HERE = $date->format('m/d/Y');
}
}
}
}
I never could get this to work correctly since there were two formats this loop could get (JSON encoded object and an array). Making it work for both was much harder than just doing it in JavaScript so instead of modifying on the server I format the data how I want client side and send it up. This formatting is purely presentational and for personal use so if someone were to put in a debugger and change the code to send a different format thats fine and there's no security issues.
So in the end, this code was rewritten in JS and since JS handles arrays and objects using the same pointers the above issue wasn't an issue any longer.

Codeigniter accessing array values

I simply want to know how to access array elements retrieved from a database. I have the following code to get the names of each item in my database.
$plat_options = $this->db->get('tblplatform_options')->select('name')->result();
How do I go about accessing the name from the array $plat_options? Typically I would do $plat_options[0] for the first element in C#, how is this done in php/codeigniter?
In PHP/Codeigniter, can be done in the same way:
$plat_options[0] //if you have this element, usually is better to check if exists.
You can retrieve all the elements with foreach($plat_options as $option){...}
You can cast to object: https://www.kathirvel.com/php-convert-or-cast-array-to-object-object-to-array/
Or use a Codeigniter Helper (assuming you are using CI3): http://www.codeigniter.com/user_guide/helpers/array_helper.html
I recomend to know which is your array format and retrieve that way (if you don't know, you can do a: var_dump($plat_options) ) to know if is an associative array.
You can use the result_array() function:
$data = $plat_options->result_array();
echo($data[0]['name']);
or:
$data = array_shift($q->result_array());
echo($data['name']);
I extracted this last part from: Codeigniter $this->db->get(), how do I return values for a specific row? that you could check too.
If you don't know a lof of CI, the best you can do is do a simple tutorial to understand how the data + ActiveRecord works.
Hope it helps!

PHP: How to retrieve extract text from Wiki API

I have the following call I do to Wiki to retrieve info about places, which users search for via an input called 'location'
// get wiki info about search term
$wiki_url = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=' . urlencode($_GET['location']);
EXAMPLE: https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=hungary
// get result of Wiki Search
$wiki_json = file_get_contents($wiki_url);
// decode data to use in php
$wiki_array = json_decode($wiki_json, true);
I've tried to get the text the same way I've done for other APIs but its not working because it seems I need the pageId to access the array where the text sits.
Is there a way to bypass this and get the text without knowing the pageID?
There are various possible solutions to this.
You could use reset() / current() against the pages property to get the first / current item in that array, or you could loop around that property with a foreach and ignore the keys. You could also use array_values() on the pages property to get force sequential indicies, or use array_keys() on it to get a list of the page ids and use those to access each item. (There are other ways).
The foreach option is going to be your best bet.
foreach($wiki_array['query']['pages'] as $page)
$page inside the loop will be the array that you're after.
You should then make sure you can deal with multiple results properly.

Calling an Array through URL

I'm looking to call an array through a URL, and I have looked through the questions but from what I've seen they're all for creating a url from an array/something of that sort.
I'm a bit new to HTML and such, but I can read and understand a decent amount. What I'm trying to do is make a shortcut on a website that allows me to call an array through the URL.
I get an error saying I can either use an int or an array, but I haven't found if I need a certain value in the url or I'm just calling the array the wrong way. I know how to make one in java/javascript and C++, as well as calling via code, but not through URL like it's saying I can.
Reason I'm looking to do this is get something done at a faster pace, and all at once instead of one by one. I'm using Google Chrome, and the website uses yuigen, as well as javascript, but the URL doesn't direct anything through PHP thatI know of.
Based on your comments, you want to take a URL like this:
http://this.com/?thisArray=1|2|5|6|10|5
and get an array of the values in thisArray. Just do this:
$theArray = explode('|', $_GET['thisArray']);
// now, $theArray is a PHP array: [1, 2, 5, 6, 10, 5]
$_GET['thisArray'] accesses your URL parameter, and explode('|', ...) splits it on the pipes.

How to search through POSTed form values using regex and return results

I'm working on a project where all of the members and their info are stored in a JSON file. I'm in the process of creating a search form and I need help on how to iterate through the members and check to see if there's an exact match or a similar match.
The members are stored in a SESSION variable:
$_SESSION['members'] = json_decode($jsonFile);
but I'm uncertain how to use regex to check for matches that are similar (and not just exact). For example, if a member's name is "Jonathan", I'd like that result to be returned even if the user searches "Jon". Is regex the correct approach? Any help will be greatly appreciated - thank you!
-Manoj
I think I'd be using a database to store the data rather than JSON so that you can use the LIKE searches, e.g.
SELECT * FROM users WHERE name LIKE 'Jon%'
If you absolutely have to use JSON you could loop through all members and use a regexp like
preg_match('/^'.$term.'.*/i', $element, $matches);
to check them all.
If the $jsonFile contents is an array of some sort, you may find preg_grep() of use, though it doesn't work on multidimensional arrays. You might have have to loop over each individual member record and grep the relevant fields yourself, something like:
foreach ($_SESSION['members'] as $idx => $member) {
... match relevant fields...
}

Categories