I am trying to iterate through the rows in a phpbb table called phpbb_posts and extract each entry in phpbb's "post_subject" column and compare its value with a predefined string in Wordpress PHP file but I am having some issues - the expressions don't evaluate to true.
My phpBB's tables are installed in WP's database so I have full access to the values.
See the code below to demonstrate the issue I am having.
function matchPhpBBTopic()
{
global $wpdb;
$wp_post_title_string = get_the_title();
$result = $wpdb->get_results("SELECT * FROM phpbb_posts");
foreach($result as $row)
{
$phpbb_post_title_array = array($row->post_subject);
$phpbb_post_title_string = implode("", $phpbb_post_title_array);
// One of the values in $row->post_subject contains
// the value in $wp_post_title_string
if (strcmp($wp_post_title_string, $phpbb_post_title_string) == 0)
{
// This line never runs but the $wp_post_title_string value
// is there, in the table, I've printed it and it's there
echo 'We found a match!<br>';
}
}
}
Any assistance would be appreciated.
So in other words, I have a topic posted in WP and I have exactly the same topic posted in phpBB and I want to iterate through the phpBB's table and when I find the topic, I want to run some code. I don't understand why the "if" expression does not run.
Couldn't you just do:
if ($wp_post_title_string == $phpbb_post_title_string) {}
I don't think strcmp() is appropriate. It converts the string to encoding numbers.
http://us1.php.net/strcmp
Also check for lower and upper case, spaces, and different encodings.
Do strtolower() and trim() first and see what you get.
Also looks like you're imploding subject and title, so don't think they'll match.
Related
I'm currently creating an Admin Panel and I'm coding it to be 100% fool proof. You'll be able to edit every piece of information through editing phrases from the database.
The main way I know is to use this:
$c = $conn->query('SELECT phrase FROM phrases WHERE phrase_name = "corp_name"') or trigger_error($conn->error);
$d = $c->fetch_array();
$phrase_corp_name = $d['phrase']; // "Corporation"
This is not very ideal when I'm going to have dozens of phrases.
The issue is, when I try to select a phrase like so, it does nothing:
$phrase_corp_name = mysqli_query($conn, "SELECT phrase FROM phrases WHERE phrase_name = 'corp_name'");
This should ideally output, "Corporation," which is the value of "phrase" in this instance. I'm assuming the code is wrong, but I can't wrap my head around it.
The error I get here is:
Recoverable fatal error: Object of class mysqli_result could not be converted to string in ... on line 94
A single variable for a single row in the database is completely unnecessary. You can use associative arrays for this purpose; they are perfect for such task.
Get all the results from your phrases table into an array and then access the phrases using keys.
$c = $conn->query('SELECT phrase_name, phrase FROM phrases') or trigger_error($conn->error);
$phrases = [];
foreach ($c as $row) {
$phrases[$row['phrase_name']] = $row['phrase'];
}
and then access the values like this:
echo $phrases['corp_name'];
As a side note, you would be better off enabling mysqli error reporting, rather than doing it manually yourself. Please read How to get the error message in MySQLi?
I am trying to learn PHP while I write a basic application. I want a process whereby old words get put into an array $oldWords = array(); so all $words, that have been used get inserted using array_push(oldWords, $words).
Every time the code is executed, I want a process that finds a new word from $wordList = array(...). However, I don't want to select any words that have already been used or are in $oldWords.
Right now I'm thinking about how I would go about this. I've been considering finding a new word via $wordChooser = rand (1, $totalWords); I've been thinking of using an if/else statement, but the problem is if array_search($word, $doneWords) finds a word, then I would need to renew the word and check it again.
This process seems extremely inefficient, and I'm considering a loop function but, which one, and what would be a good way to solve the issue?
Thanks
I'm a bit confused, PHP dies at the end of the execution of the script. However you are generating this array, could you also not at the same time generate what words haven't been used from word list? (The array_diff from all words to used words).
Or else, if there's another reason I'm missing, why can't you just use a loop and quickly find the first word in $wordList that's not in $oldWord in O(n)?
function generate_new_word() {
foreach ($wordList as $word) {
if (in_array($word, $oldWords)) {
return $word; //Word hasn't been used
}
}
return null; //All words have been used
}
Or, just do an array difference (less efficient though, since best case is it has to go through the entire array, while for the above it only has to go to the first word)
EDIT: For random
$newWordArray = array_diff($allWords, $oldWords); //List of all words in allWords that are not in oldWords
$randomNewWord = array_rand($newWordArray, 1);//Will get a new word, if there are any
Or unless you're interested in making your own datatype, the best case for this could possibly be in O(log(n))
I'm in WordPress. I need to compare two arrays, then unset any matches from one of them. The trouble is, one of the arrays is gettings its data from get_users, so I think I might have to convert it to strings using foreach, so I can tell it to give the user_login for the users in the array. Unless I'm wrong about that, I think what I need to be able to do is take the array, do a foreach statement so I can tell it to grab the user_logins, then convert them all back to an array. Here's all I have so far (and I'm not sure about whether I'm doing the if statement correctly in there (whether "null" is the right qualifier):
$adminnames = get_users('role=administrator');
$result = array_intersect($adminnames, $username);
if($result !== null){unset($username[$result]);}
$username is one of the attributes in the shortcode.
Also, forgive my fuzziness, if there's only one person in "username" does that mean it's not an array? 'Cause if so that might mess this up.
-- UPDATE --
If the only way to get the user_login of all administrators is to do a foreach then echo it, this might not even be possible.
I found a solution that works just great. I already do a preg_split on the $username attribute, so after I run that, this is what I've done to unset administrator usernames from the $username attribute:
$users = preg_split("/[\s,]+/",$username);
wp_get_current_user();
if(current_user_can(administrator)){
$nohidename = array_search($current_user->user_login,$users);
if($nohidename !== FALSE){unset($users[$nohidename]);
}
}
So it does it just based on whether the current user is an administrator. If not, it leaves it as is. Works great.
EDIT - An even simpler way to do it, without the array_search:
if(current_user_can(administrator)){
if($username){
unset($username);
}
}
A critical function in a PHP script I am debugging get's two attributes from an XML file on an external site. The attributes are labeled 'code' and 'locationCode' within a tag called Channel. The issue is that sometimes the locationCode is posted as an empty string ('') or not defined at all by the site for channels I cannot use, so I need to loop through the channels until I find a non-empty locationCode string. To do this, I created a while loop, but my current implementation does not successfully loop through the location codes. Is there a better way to implement this?
Current code:
public function setChannelAndLocation(){
$channelUrl="http://service.iris.edu/fdsnws/station/1/query?net=".$this->nearestNetworkCode.
"&sta=".$this->nearestStationCode."&starttime=2013-06-07T01:00:00&endtime=".$this->impulseDate.
"&level=channel&format=xml&nodata=404";
$channelXml= file_get_contents($channelUrl);
$channel_table = new SimpleXMLElement($channelXml);
$this->channelUrlTest=$channelUrl;
//FIXME: Check for empty locationCode string
$this->channelCode = $channel_table->Network->Station->Channel[0]['code'];
$this->locationCode = $channel_table->Network->Station->Channel[0]['locationCode'];
$i = 1;
while($this->locationCode=''){
$this->channelCode = $channel_table->Network->Station->Channel[$i]['code'];
$this->locationCode = $channel_table->Network->Station->Channel[$i]['locationCode'];
$i++;
}
}
sample XML file for code: http://service.iris.edu/fdsnws/station/1/query?net=PS&sta=BAG&starttime=2013-06-07T01:00:00&endtime=2013-10-12T18:47:09.5000&level=channel&format=xml&nodata=404
There are two problems I can see with this line:
while($this->locationCode=''){
Firstly, you have typed an assignment (=) when what you wanted was a comparison (==). So instead of testing the condition, this line is over-writing the current value of $this->locationCode and then testing the "truthiness" of '', which evaluates to false, so the while loop never runs.
Secondly, the sample XML file shows that the attribute is not in fact empty, but contains some whitespace. Assuming these are the values you want to ignore (there are none in the sample right now which have any other value), you can use trim() to eliminate the whitespace from the comparison, giving you this:
while( trim($this->locationCode) == '' ) {
I have any array
$num_list = array(42=>'0',44=>'0',46=>'0',48=>'0',50=>'0',52=>'0',54=>'0',56=>'0',58=>'0',60=>'0');
and I want to change specific values as I go through a loop
while(list($pq, $oin) = mysql_fetch_row($result2)) {
$num_list[$oin] = $pq;
}
So I want to change like 58 to 403 rather then 0.
However I always end up getting just the last change and non of the earlier ones. So it always ends up being something like
0,0,0,0,0,0,0,0,0,403
rather then
14,19,0,24,603,249,0,0,0,403
How can I do this so it doesn't overwrite it?
Thanks
Well, you explicititly coded that each entry should be replaced with the values from the database (even with "0").
You could replace the values on non-zero-values only:
while(list($pq, $oin) = mysql_fetch_row($result2)) {
if ($pq !== "0") $num_list[$oin] = $pq;
}
I don't get you more clear, i thought your asking this only. Check this
while(list($pq, $oin) = mysql_fetch_row($result2)) {
if($oin==58) {
$num_list[$oin] = $pq;
}
}
In my simulated tests (although You are very scarce with information), Your code works well and produces the result that You want. Check the second query parameter, that You put into array - namely $pg, thats what You should get there 0,0,0,0,0...403 OR Other thing might be that Your $oin numbers are not present in $num_list keys.
I tested Your code with mysqli driver though, but resource extraction fetch_row is the same.
Bear in mind one more thing - if Your query record number is bigger than $numlist array, and $oin numbers are not unique, Your $numlist may be easily overwritten by the folowing data, also $numlist may get a lot more additional unwanted elements.
Always try to provide the wider context of Your problem, there could be many ways to solve that and help would arrive sooner.