How can you make whether case 1 goes to the next result case 2 goes to the previous result
Thanks a lot (I'm still a little new, any help will be received)
<?php
$search = mysqli_query($mysqli,"SELECT * FROM `Products` ORDER BY `Products`.`abc_phone` ASC");
while($AnswerP = mysqli_fetch_array($search)){
/*code...
...
*/
$next = $agi->get_data($FolderToPlay."next_and_oder",7000,1)['result'];
switch ($next){
case 1:
next...;
break;
case 2:
previous...;
break;
case 3:
oder();
break;
}
}
?>
I have a query fetching product ID from the database, then base on the ID #, I have a switch statement that gets the image for the product. All works fine and at the end of the switch I have a default case where it would just print out an image that says "no image available".
Here is part of the code for imagequery.php
<?php
if($image = mysqli_fetch_assoc($imagequery))
{
switch($image['product_code'])
{
case 0:
echo '<th><img src="Images/pencil.jpg">';
break;
case 1:
echo '<th><img src="Images/pen.jpg">';
break;
case 2:
echo '<th><img src="Images/marker.png">';
break;
case 3:
echo '<th><img src="Images/clipper.png">';
break;
case 4:
echo '<th><img src="Images/paper.png">';
break;
default:
echo '<th><img src="Images/noimage.jpg">';
break;
}
}
Edit1: After doing more recommended echo checks, turns out the $image['imagequery'] is going in ascending order and I'm not sure why. My code to get $imagequery is this.
$results = "SELECT product_code FROM products WHERE product_desc='Office Materials'";
$imagequery = mysqli_query($mysqli, $results);
include('imagequery.php');
I have a database table that is called roles. and it contains 5 roles and has id from 1 to 5, I have gotten the code below to work with 1 id only but want to add more without re using the entire block. so if id equals 3,4,5 show the link. can I add multiple id's in the if?
while ($row = mysqli_fetch_assoc($result)) {
$RoleID = $row['RoleID'];
if ($RoleID == 3) {
echo '<li><span>Admin Area</span></li>';
}
}
I guess you can use in_array:
$admin_array = [3,4,5];
while ($row = mysqli_fetch_assoc($result)) {
if (in_array($row['RoleID'], $admin_array) {
echo '<li><span>Admin Area</span></li>';
}
}
Hope below code will help you
I have used in_array() function
while ($row = mysqli_fetch_assoc($result)) {
$RoleID = $row['RoleID'];
if (in_array($RoleID,array(3,4,5))) {
echo '<li><span>Admin Area</span></li>';
}
}
Something that will give some added flexibility when say you want an id to do something similar but maybe end in the same way is to use a switch case.
while ($row = mysqli_fetch_assoc($result)) {
switch($row['RoleID']) {
case 1:
//Id 1 logic only
break;
case 2:
//Id 2 logic only
break;
case 3:
//Without a break it will fall through.
//Nice because if for some reason you need
//custom for individual id do it with no break
case 4:
case 5:
echo '<li><span>Admin Area</span></li>';
break;
default:
//Do something here for instances when a ROLE ID is not found
}
}
I have the following PHP code on my site which basically accesses a PHP script that pulls out information from a mysql table;
<?php include('event_connect.php'); ?>
Is it possible to include a reference to a specific section of code in this script?
Basically I have to create 39 different scripts for individual dropdown e.g. in the "XXX" drop down, only records with an identifier "xxx" will show up.
What i thought would be easier than createing 39 scripts is (if possible) just add some sort of reference value to the above code depending on where in the page it is placed. Kind of like how VBA allows you to call a selected function from another...
Edit: This is the PHP i use to access the mysql database, with the code provided by niet added in - works a treat! Thank you.
<?php
//Get te CSS styling
// Make a MySQL Connection
mysql_connect("xxxx", "xxxx", "xxxx", "xxxx") or
die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());
// Retrieve all the data from the "xxxx" table
switch($section) {
case 1:
$result = mysql_query("SELECT * FROM `Events_List` WHERE `Value` = 'value_1' LIMIT 0 , 30") or die(mysql_error());
break;
case 2:
$result = mysql_query("SELECT * FROM `Events_List` WHERE `Value` = 'value_2' LIMIT 0 , 30") or die(mysql_error());
break;
// store the records of the "xxxx" table into $row array and loop through
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row['name'];
$Event = $row['Event'];
$Date = $row['Date'];
$Type = $row['Type'];
$Description = $row['Description'];
$Event = htmlspecialchars($row['Event'],ENT_QUOTES);
$Date = htmlspecialchars($row['Date'],ENT_QUOTES);
$Type = htmlspecialchars($row['Type'],ENT_QUOTES);
$Description = htmlspecialchars($row['Description'],ENT_QUOTES);
echo " <div id='ev_con'>
<div id='ev_img'> </div>
<div id='Event'> $Event </div>
<div id='Date'> $Date </div>
<div id='Type'> $Type </div>
<div id='Description'> $Description </div>
</div>";}
?>
This runs one query fine, but having trouble getting multiple queries running..
Maybe something like this?
$section = 1;
include("event_connect.php");
And in your event_connect file:
switch($section) {
case 1:
// do things
break;
case 2:
// do other things
break;
// ...
}
how about...
function get_event_connect($eventID){
switch($eventID){
case "1" :
// output code
break;
case "2" :
//output code
break;
}
}
$eventID = "1";
echo get_event_connect($eventID);
function addAds($n) {
for ($i=0;$i<=$n;$i++) {
while($row=mysql_fetch_array(mysql_query("SELECT * FROM users"))) {
$aut[]=$row['name'];
}
$author=$aut[rand(0,mysql_num_rows(mysql_query("SELECT * FROM users")))];
$name="pavadinimas".rand(0,3600);
$rnd=rand(0,1);
if($rnd==0) {
$type="siulo";
} else {
$type="iesko";
}
$text="tekstas".md5("tekstas".rand(0,8000));
$time=time()-rand(3600,86400);
$catid=rand(1,9);
switch ($catid) {
case 1:
$subid=rand(1,8);
break;
case 2:
$subid=rand(9,16);
break;
case 3:
$subid=rand(17,24);
break;
case 4:
$subid=rand(25,32);
break;
case 5:
$subid=rand(33,41);
break;
case 6:
$subid=rand(42,49);
break;
case 7:
$subid=rand(50,56);
break;
case 8:
$subid=rand(57,64);
break;
case 9:
$subid=rand(65,70);
break;
}
mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
}
echo "$n adverts successfully added.";
}
The problem with this function, is that it never loads. As I noticed, my while loop causes it. If i comment it, everything is ok. It has to get random user from my db and set it to variable $author.
The problem is that the query is in the loop, so it gets run every time (so you start from the beginning every time). Just move the mysql_query() part to right before the while loop and store it in a variable:
$query = mysql_query("SELECT * FROM users");
while($row=mysql_fetch_array($query))
You can replace this mega switch with one line:
$subid = rand(($catid * 8) - 7, min($catid * 8, 70));
The condition of a while loop is executed and evaluated with each iteration. So mysql_query is called with every iteration and retunrs true.
Just execute your database query once and cache the result:
function addAds($n) {
$result = mysql_query("SELECT * FROM users");
$aut = array();
while ($row = mysql_fetch_array($result)) {
$aut[]=$row['name'];
}
$rowCount = count($aut);
for ($i=0; $i<=$n; $i++) {
$author=$aut[rand(0,$rowCount)];
// …
mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
}
echo "$n adverts successfully added.";
}
I also think the problem is your functions are way too big to understand(quickly). You should make them smaller and test them with a unit testing framework like phpunit.
It's a lot of time that I don't use PHP but I think that the assignment
$row=mysql_fetch_array(mysql_query("SELECT * FROM users"))
should always returns true, it executes the query again and again on every iteration..
You're starting a new query each time you run the loop.