Optimize existing code and need to list alphabetically - php

I need help optimizing the code to run faster, unless it is optimized the best.
I also want to alphabetize the list and I am unsure how to do that.
It should be alphabetized by $userinfo[0]["sn"][0]
I am using the adLDAP class: http://adldap.sourceforge.net/
<?php
require_once('adLDAP.php');
//header('Content-type: text/json');
$adldap = new adLDAP();
$groupMembers = $adldap->group_members('STAFF');
//print_r($groupMembers);
$userinfo = $adldap->user_info($username, array("givenname","sn","telephonenumber"));
$displayname = $userinfo[0]["givenname"][0]." ".$userinfo[0]["sn"][0];
print "<ul>";
foreach ($groupMembers as $i => $username) {
$userinfo = $adldap->user_info($username, array("*"));
$displayname = "<strong>".$userinfo[0]["givenname"][0]." ".$userinfo[0]["sn"][0]."</strong> - ".$userinfo[0]["telephonenumber"][0];
if($userinfo[0]["sn"][0] != "" && $userinfo[0]["givenname"][0] != "" && $userinfo[0]["telephonenumber"][0] != "") {
print "<li>".$displayname."</li>";
}
}
print "</ul>";
?>

I dont know about optmizing but to order your array you can use an example on PHP help page called multi_sort:
link text
There is a comment regarding code injection on the example provided but you can workaround that I think.

Related

PHP array selection not working correctly

I am working on a website that alternates section depending on the ID, I created this code but noticed one of my parameters is not working correctly
$fb_prac = array('34565', '34565', '1212', '1192', '1219', '1180', '1234','1186', '1221');
if (get_the_ID() != ('34565' or '34565' or '1212' or '1192' or '1219' or '1180' or '1234' or '1221')) {
include 'facebook.html';}
else {
if (get_the_ID() == '1186') {
include 'facebook-1186.html';}else{
echo do_shortcode('[facebook_card]');}}
The ID pointing to 1186 is ok and the else echoing the shot code is ok BUT the array wit the IDs pointing to facebook.html do not load. Is there something wrong with my syntax? Any help is appreciated
Did you try to use the in_array function?
if (in_array(get_the_ID(), $fb_prac)) {
include 'facebook.html';
}
https://www.php.net/manual/en/function.in-array.php
I'm reading the logic like this, if the id matches 1186 include facebook-1186.html, if the id is NOT in your array $fb_prac, then include facebook.html. Else do the shortcode.
<?php
$fb_prac = array('34565', '34565', '1212', '1192', '1219', '1180', '1234','1186', '1221');
$id = get_the_ID();
if ($id == '1186') {
include 'facebook-1186.html';
}
elseif (!in_array($id, $fb_prac)) {
include 'facebook.html';
}
else {
echo do_shortcode('[facebook_card]');
}

php check empty or null in many variables

Hello I have a simple script
This is my script, and try this script
<?php
$user_id = $_REQUEST['user_id'];
$pid = $_REQUEST['pid'];
$nopr = $_REQUEST['nopr'];
$tglpr = $_REQUEST['tglpr'];
$uraianpr = $_REQUEST['uraianpr'];
$jenispr = $_REQUEST['jenispr'];
$nilaipr = $_REQUEST['nilaipr'];
$costproject = $_REQUEST['costproject'];
$remarkpr = $_REQUEST['remarkpr'];
$tmpattachid = $_REQUEST['tmpattachid'];
if ($user_id==NULL)
{
$error "User Id Not Complete";
}
else if ($pid==NULL)
{
$error= "PID Not Complete";
}
echo $error;
?>
I want if $user_id or other variables is empty/null
i confused when other variable have empty/null data
i must typing code many if statement :(
example : 5 variables ($pid,$nopr,$tglpr,$jenispr,$costproject) is empty/null
this show error like this
PID Not Complete
NoPR Not Complete
Tgl Pr Not Complete
Jenis Pr Not Complete
Cost Project Not Complete
Help Me, Thank's.
I won't give you the full answer, as stated in comments, this is a basic basic concept you need to learn, but I will show you the template.
You want to use a FOREACH loop to check each value in the array. See http://php.net/manual/en/control-structures.foreach.php
And Set it out like this:
foreach($_REQUEST as $key=>$row){
if (is_null($row) || empty($row)){
$errorText .= "You have no data in your ".$key."<br>";
}
}
unset($key,$row);
Then you can output the $errorText value telling people which rows should be fixed. Easy.
Loop through the $_REQUEST and find the empty or null key.
CODE :
foreach ($_REQUEST as $key => $value) {
if(trim($value) ==='' || is_null($value))
{
echo $key . " is not complete" . "<br/>";
}
}

Code causing page to render blank with PHP

So this is the first PHP script (if it's even called that?) that I've ever written from scratch, and I'm having an issue in that when it's applied to the existing (and otherwise working) page, the page shows up blank. I was hoping one of the many people who are better and more experienced than I am can take a look and find what is no doubt a blatant syntax error. Thank you in advance to anyone that shows me the light!
<?php
$sql = "SELECT * FROM 'jos_downloads_files'";
$rows = $db->fetch_all_array($sql);
foreach($rows as $row) {
$filename = $row['filetitle'];
$filepath = $row['realname'];
$featured = $row['featured'];
$id = $row['containerid'];
}
foreach ($id as $containername) {
if ($id == 2) {
$containername ="Incidental Information";
}
if ($id == 3) {
$containername ="Monitoring Reports";
}
if ($id == 4) {
$containername ="Agendas";
}
if ($id == 5) {
$containername ="Decision Prep";
}
if ($id == 6) {
$containername ="Agendas";
}
if ($id == 7) {
$containername ="Policy Governance";
}
echo '<div class = "moduletable">
<h3>' . $containername . '</h3>';
foreach ($featured as $featureedtrue) {
if ($featuredtrue == 1) {
echo '<ul class="weblinks">
<li>
<a>' . $filename . '</a>
</li>';
}
}
}
?>
As celeriko mentioned, you never declared $db prior to using it. As Xatenev mentioned, I'm not sure what fetch_all_array is, perhaps fetchAll()? Finally, as Valery Statichny mentioned, $id will never be an array.
In the future, it is helpful to turn on error reporting so that you can see where your scripts are crashing. You can do so by adding the following:
ini_set('display_errors',1);
error_reporting(E_ALL);
In production environments, turn error reporting off so that users don't see error messages.
Edit
More from Xatenev:
Why are you looping foreach $featured? $featured can only be ONE entry at the moment. You have to write $featured[] = $row['featured']; to get an array where u can loop through. Same to $id. 5. You are looping through $featured but use $filename then? $filename will print an array EVERY TIME you loop through $featured. So for example when you have 100 entries in $featured, you have 100x the same content when you print out $filename. – Xatenev

How to make a php variable $_POST upon clicking it?

I'm trying to make $suggestion[$ss_count] become a clickable link that does a $_POST once it is clicked. I'm trying to achieve a query expansion sort of an effect. The word itself of course needs to be the information posted and it needs to be recognized as
if ($_POST['query'])
The code I use for this is:
<?php
if ($_POST['query'])
{
$query = urlencode($_POST['query']);
$s_count = 0;
$ss_count = 0;
$query = 'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/'.$query.'? key=135a6187-af83-4e85-85c1-1a28db11d5da';
$xml = new SimpleXMLIterator(file_get_contents($query));
foreach ($xml -> suggestion as $suggestion[$s_count])
{
$s_count++;
}
if ($s_count > 1)
{
echo ('<h4>Did you mean?</h4>');
while ($ss_count <=$s_count)
{
echo ($suggestion[$ss_count].'<br>');
$ss_count++;
}
}
}
?>

converting a chunk of repeated php into a function?

I have little programming experience so the jump from basic php to using classes, functions etc is a little daunting.
I'm building a little php script that works out what "theme" a user has selected via a form and from that selection, it loads some specific files. I've built a working script, but there's so much repetition in it, it's a joke.
// Add our theme names to variables
$theme1 = "theme1";
$theme2 = "theme2";
$theme3 = "theme3";
// Test to see what Theme the user chose.
// Theme 1
if ($themeChoice==$theme1)
{
// Load the theme
$homepage = file_get_contents('../themes/'.$theme1.'/index.html');
$mobile_js_main = file_get_contents('../themes/'.$theme1.'/js/main.js');
$mobile_js_jquery = file_get_contents('../themes/'.$theme1.'/js/jquery.js');
$mobile_css_easy = file_get_contents('../themes/'.$theme1.'/css/easy.css');
$mobile_images_bg = file_get_contents('../themes/'.$theme1.'/images/bg.png');
$mobile_images_footer = file_get_contents('../themes/'.$theme1.'/images/footer.png');
$mobile_images_nav = file_get_contents('../themes/'.$theme1.'/images/nav.png');
if ($AddPortfolioPage != '')
{
$portfolioPage = file_get_contents('../themes/'.$theme1.'/portfolio.html');
}
if ($AddContactPage != '')
{
$ContactPage = file_get_contents('../themes/'.$theme1.'/contact.html');
}
if ($AddBlankPage != '')
{
$blankPage = file_get_contents('../themes/'.$theme1.'/blank.html');
}
}
This is then repeated for each theme...which is obviously not an ideal method of doing this. Any help is much appreciated!
Put all of your themes into an array and loop to find the chosen one.
$themes = array( "theme1", "theme2", "theme3");
foreach( $themes as $theme)
{
if( $themeChoice == $theme)
{
$homepage = file_get_contents('../themes/'.$theme.'/index.html');
$mobile_js_main = file_get_contents('../themes/'.$theme.'/js/main.js');
$mobile_js_jquery = file_get_contents('../themes/'.$theme.'/js/jquery.js');
$mobile_css_easy = file_get_contents('../themes/'.$theme.'/css/easy.css');
$mobile_images_bg = file_get_contents('../themes/'.$theme.'/images/bg.png');
$mobile_images_footer = file_get_contents('../themes/'.$theme.'/images/footer.png');
$mobile_images_nav = file_get_contents('../themes/'.$theme.'/images/nav.png');
if ($AddPortfolioPage != '')
{
$portfolioPage = file_get_contents('../themes/'.$theme.'/portfolio.html');
}
if ($AddContactPage != '')
{
$ContactPage = file_get_contents('../themes/'.$theme.'/contact.html');
}
if ($AddBlankPage != '')
{
$blankPage = file_get_contents('../themes/'.$theme.'/blank.html');
}
break; // Break will exit the loop once the choice is found
}
}

Categories