Im trying to create this thing that if its get nothing from a array it would bring up a default picture but its not working out the way i want it to. It using every array but i want to separate each away.
$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."' ");
$d_name = array();
$d_amount = array();
$d_cover = array();
while(($row = mysql_fetch_assoc($result))) {
$d_name[] = $row['name'];
$d_mode[] = $row['mode'];
$d_amount[] = $row['amount'];
$d_cover[]= $row['cover'];
}
$p_cover = implode($d_cover) ;
if ( $p_cover == "") {
$p_cover = "cpages/img/default_pic.jpg";
}
else
{
$p_cover= "userdata/".$user_data['username']."/profilepic/".$p_cover;
}
<img style="margin:2pt" src="<?php echo $p_cover ?>" height="250" width="200" />
$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."' ");
while ($row = mysql_fetch_assoc($result)) {
$d_name = $row['name'];
$d_mode = $row['mode'];
$d_amount = $row['amount'];
$d_cover = $row['cover'];
if ( $d_cover == "") {
$p_cover = "cpages/img/default_pic.jpg";
}
else
{
$p_cover= "userdata/".$user_data['username']."/profilepic/".$d_cover;
}
echo '<img style="margin:2pt" src="'.$p_cover.'" height="250" width="200" />';
}
Related
please help, i am learning json, but i find it difficult to insert code echo. If you know the answer please help me
My Code
if ($result) {
$arr['news'] = '';
$idterakhir = '';
$query = $pdo->query($sql);
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
// change this into echo
$arr['news'] .= '<div class="card" news_id="'.$row->news_id.'">'.$row->news_id.'<br>'.$row->news.'</div>';
// change this into echo
$idterakhir = $row->news_id;
}
$arr['idterakhir'] = $idterakhir;
$arr['end'] = false;
} else {
$arr['end'] = true;
}
echo json_encode($arr);
into :
echo "
<div class='card' news_id='$id'> $id <br> $title </div>
";
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
$id2 = $row['news_id'];
$id = <div class="card" news_id="'.$id2.'">
$news = $row['news'];
$title = $id.$id2.<br>'.$news.'</div>
$arr['news'] .= title;
echo $arr['news'];
$idterakhir = $row->news_id;
}
This should print $arr['news'] for each circle of the while.
If you need to echo out the contents of $arr['news'], just do:
echo $arr['news'];
In my php code below am trying to fetch a multiple text character from database and str_split each result output. add USED to the end line of each output and output remaining characters also i have tried:
require("init.php");
echo "<form action='reg.php' method='post'>
<b>booking code:</b><br />
<input type='text' name='book' size='17' />
<input type='submit' name='login' maxlength='2' size='2' value='PRINT OUT' />
</form>";
if(isset($_POST["login"]))
{
$book = mysqli_real_escape_string($conn, $_POST["book"]);
$sql = "SELECT `item_name` , `quantity` FROM `books` WHERE `book`='$book'"; $query = mysqli_query($conn, $sql);
while ($row1 = mysqli_fetch_array($query)) {
$d[] = $row1["item_name"];
$v[] = $row1["quantity"];
foreach($d as $r);
foreach($v as $ro);
$get = array(0 => array("yes" => $r, "no" => $ro));
foreach ($get as $item) {
$getd = $item["yes"];
}
$sql = mysqli_query($conn, "SELECT * FROM promo WHERE code='$getd' LIMIT 1");
while ($row = mysqli_fetch_array($sql)) {
$type = $row["name"];
$code = $row["recharge"];
$deta = $row["details"];
$logo = $row["logo"];
$price = $row["price"];
if(preg_match('/(xyz)/i', $type)) $type = "20";
if(preg_match('/(log)/i', $type)) $type = "16";
}
$id = $item["no"];
$result = str_split($code, $type);
$a = "";
$c = "";
for($b = 0; $b<$id; $b++)
{
$a .= $result[$b];
}
$b = "USED";
for($bn = $id; $bn<sizeof($result); $bn++){
$c .= $result[$bn];
}
echo $a.$b.$c;
}
}
}
and all my result output with code is unknow am saying all result all complecated result output thanks for your time and impact in my solutions
I am a completely newbie in programming php I would like to make this code below return many arrays(to flash as3), however I only receive one array.Can anyone please pinpoint what is my mistake here? thanks.
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
echo "returnStr=$data_array";
exit();
}
When you write your exit insight your loop you stop executing your program and you get only one record. You should set the echo and exit after your while loop.
$data_array = "";
$i = 0;
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql)) {
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1) {
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
} else {
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
Those two last line of your should be outside of your loop:
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
If you would name the columns that you want in the SELECT then it's much simpler. Make sure to use MYSQLI_ASSOC in the fetch:
$sql = mysqli_query($conn, "SELECT Username, Fb_id, Access_token, Fb_sig, Char_id FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC))
{
$data_array[] = implode('|', $row);
}
echo "returnStr=" . implode('(||)', $data_array);
exit();
I have a function that retrieve list of friends from the MySQL table using the friend_array field.
But the problem is that the browser doesn't show more than one picture, but it shows the default one.
this is the code
<?php
//***********************Displaying Friend List*************************//
$friendList = "";
$friendListTitle="";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
/*$frnd1 = $friendArray[0];
$frnd2 = $friendArray[1];
/*$frnd3 = $friendArray[2];
$frnd4 = $friendArray[3];
$friendList .='<div style="background-color:"#CCC";>'.$frnd1.'<br />'.$frnd2.'</div>';*/
$i=0;
$friendList ='<div style="background-color:"#CCC"; >';
foreach($friendArray as $key => $value)
{
$i++;
$check_pic = "members/$value/image01.jpg";
if(file_exists($check_pic))
{
$frnd_pic = '<img src = \"$check_pic\" width = "52px" border = "1"/>';
}
else
{
$frnd_pic = '<img src = "members/0/image01.jpg" width = "52px" border = "1"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value'LIMIT 1") or die(mysql_error());
if($row = mysql_fetch_array($sqlName,MYSQL_ASSOC))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList = '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
}
}
$friendList.='</div>';
}
?>
you images and divs that contain the records of friends is being replaced by next record so it display the last record of your friends.
try this may help you
$friendList = "";
$friendListTitle="";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
var_dump($friendCount);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
$i=0;
$friendList ='<div style="background-color:"#CCC"; >';
foreach($friendArray as $frndlist => $value)
{
$i++;
$check_pic = 'members/'.$value.'/image01.jpg';
if(file_exists($check_pic))
{
//storing each friend images separately
$frnd_pic[$i] = '<img src = "'.$check_pic.'" width = "52px" border = "1"/>';
}
else
{
$frnd_pic[$i] = '<img src = "members/0/image01.jpg" width = "52px" border = "1"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value'LIMIT 1") or die(mysql_error());
if ($row = mysql_fetch_array($sqlName, MYSQL_ASSOC))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList .= '<div title="'.$fname.' '.$lname.'">'.$frnd_pic[$i].'</div>';
}
}
$friendList.='</div>';
}
1) I want to save case 1 value to an array. Return this array and pass it to a function. The code become case 2, but no result come out, where is the problem?
2) In function display_urls, i want to echo both $url and $category. What should i do in IF condition or add another line of code?
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url."";
echo "".$category."";
}
}
echo "";
}
case 1: work fine
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$i=0;
echo "";
while( $row = oci_fetch_array($result) ){
$i++;
echo "";
echo "".$row['USERNAME']."";
echo "".$row['BM_URL']."";
echo "".$row['CATEGORY']."";
echo "";
}
echo "";
case 2:
$url_array = array();
while( $row2 = oci_fetch_array($result, OCI_BOTH)){
$i++;
$url_array[$count] = $row[0];
}
return $url_array;
I think you probably want something like this:
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url['BM_URL']."";
echo "".$url['CATEGORY']."";
}
}
echo "";
}
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$url_array = array();
while( $row = oci_fetch_array($result, OCI_ASSOC)){
$url_array[] = $row;
}
display_urls($url_array);
This will store all the information on the URLs in $url_array with a lookup by column name.