I have been working on this issue for what feels like endless hours, but I feel like I'm really close to solving it.
I have stripped my site down to the bare minimum code to troubleshoot this, and I think I have found the line that is causing me trouble, but I just don't know WHY it is causing me trouble. Here is my code as it is now:
//display all products
if (!isset($_GET['id'])) {
$result = mysqli_query($link, "SELECT products.*, company.company-name FROM products INNER JOIN company ON products.company-id=company.id");
if (!$result) {
echo 'Error';
exit();
}
}
//display one product
if (isset($_GET['id'])) {
$id = $_GET['id'];
$get_item_info = mysqli_query($link, "SELECT products.*, company.company-name FROM products INNER JOIN company ON products.company-id=company.id WHERE products.id='$id'");
$row = mysqli_fetch_assoc($get_item_info);
if (!$row) {
echo 'Error';
exit();
}
$company_name = $row['company-name'];
$model_abbreviation = $row['model-abbreviation'];
if ($row['model-version'] == NULL) { $image_directory_name = "$company_name/$model_abbreviation/"; } else $image_directory_name = "$company_name/$model_abbreviation (" . $row['model-version'] . ")/";
}
if (!isset($_GET['id'])) {
$group = array();
while ($row = mysqli_fetch_assoc($result)) {
$group[ $row['company-name'] ][] = $row;
}
foreach ($group as $company_name => $models) {
foreach ($models as $model) {
echo "<a href='$model[id]/'>$model[model-abbreviation]</a>";
}
}
}
if (isset($_GET['id'])) {
$top_thumb = "$image_directory_name" . "top.jpg";
echo "<img src='$top_thumb'>"; echo '</br>';
$images = glob($image_directory_name."*_thumb*");
if (!$images) {
echo 'No images to display.';
}
else {
foreach ($images as $image) {
$full = str_replace('thumb','full',$image);
echo '<img src="'.$image.'">';
}
}
}
So if I use this code in combination with my rewriterule:
RewriteRule ^products/(.*)/$ /products/index.php?id=$1
And I go to a product page, such as localhost/products/30/
No images display, HOWEVER, in the page source I can see that it is TRYING to display the images (though I get no error from my script, just a blank page), just with the wrong path - it looks like this:
<img src='Company Name/Model Name (Model Version)/top.jpg'></br><img src="Company Name/Model Name (Model Version)/A_thumb.jpg">
So the code is working, but the path is wrong. OK.
So I added /products/ to the $image_directory_name variable in this line:
if ($row['model-version'] == NULL) { $image_directory_name = "/products/$company_name/$model_abbreviation/"; } else $image_directory_name = "/products/$company_name/$model_abbreviation (" . $row['model-version'] . ")/";
This is where it breaks.
The top thumb is fixed and displays, but I get my error when trying to glob() the bottom thumbs - 'No images to display'. And the source doesn't even show as trying to load them anymore.
So. I have gotten really close, I've spent countless hours on this. Anyone got any idea what's going on? Something to do with my $image_directory_name path or the glob() code I'm guessing...I am so mentally desperate to solve this.
edit: additional info: When I go to localhost/products/companyname/productname/ in my browser, I get the Error from the if (!$row) line in my code. But I go to localhost/products/companyname/productname/imagename.jpg, the image loads.
update: I even tried moving all of my images from /products/companyname/productname/
to /images/companyname/productname/
The bottom thumbs still don't show up. Argh.
Related
I am having issue data Array overwritten in foreach loop. Result I am getting like this wrongRight together .Right answer is showing but also wrong for example ZucchiniCauliflower.Please help
CODE 1
$data = array();
$dis_07= null;
$dis_03 = null;
if (is_array($row)) {
foreach ($row as $value) {
$gccat_id = $value->gccat_id;
$ccat_id = $value->ccat_id;
$cat = $value->cat_id;
if (isset($gccat_id) && $gccat_id == $id) {
$dis_07 = $value->category;
$dis_02 = $value->child_id;
}
if (isset($ccat_id) && $ccat_id == $id) {
$dis_03 = $value->category;
$dis_02 = $value->parent_id;
}
}
}
$data['Dis_03'] = $dis_03;
$data['Dis_07'] = $dis_07;
if (isset($data['Dis_03'])) {
echo $data['Dis_03'];
}
if (isset($data['Dis_07'])) {
echo $data['Dis_07'];
}
First I tried this way But In one I was getting right in second link I am getting right So Tried the code previous one .In the prvious I am getting correct and wrong one together EExample ZucchiniCauliflower
CODE 2
if (isset($id)) {
$db = Database::newInstance();
$data = array();
$data['cat_status'] = 1;
$sql = "SELECT * FROM category WHERE cat_status=:cat_status ";
$row = $db->read($sql,$data);
$data['id'] = $crypt->decryptId($id);
echo $data['id'];
$id=$data['id'];
if (is_array($row)) {
foreach ($row as $value) {
$gccat_id=$value->gccat_id;
$ccat_id = $value->ccat_id;
$cat = $value->cat_id;
if (isset($gccat_id) && $gccat_id == $id) {
$data['Dis_03']=$value->category;
}
if (isset($ccat_id) && $ccat_id == $id) {
$data['Dis_03'] = $value->category;
break;
}
}
}
}
--------------------------READ FROM HERE------------------------
Here is a link one when I click on this link
$id=$value11->gccat_id;
$title
I expected the output is
Home>Raspberry
Here is a link Second link when I click on this link
Here id is ($value11->gccat_id)
window.open('<?= BASEURL ?>ap/'+id,'_self');
I expected the output is
Home>Cauliflower
1. WHEN I Use the Code 2 (Added break in this condition
(isset($ccat_id) && $ccat_id == $id)) Then click on link second
it gives output Home>Cauliflower which I was expecting. It is
correct.
2. But this time as I added the break in (isset($ccat_id) && $ccat_id == $id). I click on link one It gives wrong output which I was not expecting. Home>Squash which is wrong.
In one link I was expecting
Home>Cauliflower
ERROR NOTE If I add Break; then link Second gives correct output but when I remove Break; then link one give correct. I wanted Both link should give correct output.
Problem was with cat_id,ccat_id ,gccat_id.
I provided 8 digit unique number with the following output,now I am getting the correct output.
function generateUniqueNumber() {
return sprintf('%08d', mt_rand(1, 99999999));
}
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
I'm looking for a way to do a wildcard search when searching for accounts in SugarCRM but I'm having trouble getting the queries to work properly.
Here's the build_generic_where_clause() function:
function build_generic_where_clause ($the_query_string) {
$where_clauses = Array();
$the_query_string = $this->db->quote($the_query_string);
array_push($where_clauses, "accounts.name like '%$the_query_string%'");
if (is_numeric($the_query_string)) {
array_push($where_clauses, "accounts.phone_alternate like '%$the_query_string%'");
array_push($where_clauses, "accounts.phone_fax like '%$the_query_string%'");
array_push($where_clauses, "accounts.phone_office like '%$the_query_string%'");
}
$the_where = "";
foreach($where_clauses as $clause)
{
if(!empty($the_where)) $the_where .= " or ";
$the_where .= $clause;
}
$log = fopen('1.txt', "a");
fwrite($log, $the_where . "\n");
return $the_where;
}
I only changed array_push($where_clauses, "accounts.name like '%$the_query_string%'"); to include the percentage signs on either side of the_query_string.
Here's processSearchForm() from view.list.php:
function processSearchForm(){
if(isset($_REQUEST['query']))
{
// we have a query
if(!empty($_SERVER['HTTP_REFERER']) && preg_match('/action=EditView/', $_SERVER['HTTP_REFERER'])) { // from EditView cancel
$this->searchForm->populateFromArray($this->storeQuery->query);
}
else {
$this->searchForm->populateFromRequest();
}
$where_clauses = $this->searchForm->generateSearchWhere(true, $this->seed->module_dir);
if (count($where_clauses) > 0 )$this->where = '('. implode(' ) AND ( ', $where_clauses) . ')';
$GLOBALS['log']->info("List View Where Clause: $this->where");
$log = fopen('1.txt', "a");
fwrite($log, $this->where . "\n");
}
if($this->use_old_search){
switch($view) {
case 'basic_search':
$this->searchForm->setup();
$this->searchForm->displayBasic($this->headers);
break;
case 'advanced_search':
$this->searchForm->setup();
$this->searchForm->displayAdvanced($this->headers);
break;
case 'saved_views':
echo $this->searchForm->displaySavedViews($this->listViewDefs, $this->lv, $this->headers);
break;
}
}else{
echo $this->searchForm->display($this->headers);
}
}
Note that I only added the log file write to catch the $this->where. If I use the searchbox to find an account such as "Newbold" as well as "New York Design", I only get "Newbold" as a result and my log file reads (accounts.name like 'new%'). So the first percentage sign is being removed somehow or another, I believe in the processSearchForm() somewhere. It's tough to figure out if that's the case or if the culprit lies elsewhere. I find this code to be a bit convoluted and all over the place, but this is the only customization I need done. Any help would be immensely appreciated.
You should be able to do this without changing any code. When you're searching from the Accounts list view, simply add the wildcards to your search in the form. Put '%$the_query_string%' in the search form.
If you're wanting to programmatically use a wildcard to search for records, you can do something like the following.
<?php
$filter = 'ABC%'; // account names that start with 'ABC'
$account = BeanFactory::getBean('Accounts');
$accounts = $account->get_list("", "accounts.name like '".$filter."'");
foreach ($accounts['list'] as $account)
{
// do something with $account
}
get_list() will pull what you need. One thing to note, get_list() will only return the numbers of records that you have your list views to return. So if your list views only show 20 records, get_list() will return up to 20 records. If you want to get all results, use get_full_list().
So, right now I'm trying to generate dynamic URLs as well as their paths without using .htaccess or the use of modules. Basically, I'm trying to rewrite and output URL paths that work statelessly on different localhosts served by Apache.
I would like to have an index.php that gets URIs through a query string like this (obviously not code):
-> index.php (echos dynamically generated URL hyperlinks)
-> index.php?q=/ (echos every URI in JSON)
-> index.php?q=/some_id (outputs table some_id info w/ links to reviews)
-> index.php?q=/some_id/reviews (outputs table column of reviews w/ link to review_ids)
-> index.php?q=/some_id/reviews/review_id (output related column info)
Could someone walk me through how I'd go about doing this? I figure I'm going to have to write the URL using $_SERVER methods and explode while iterating through an array of table IDs..?
Any help is greatly appreciated!
EDIT:
Here's the code I was trying to write :/
<?php
$db = $user = $pw = 'logininfo';
try {
$dbconn = new PDO('mysql:host=localhost;db='.$db, $user, $pw;
}
catch (Exception $e) {
echo "Error: ";
echo $e->getMessage();
}
?>
<!DOCTYPE HTML>
<head>
<title>Product Reviews</title>
</head>
<body>
<h1>Product List:</h1>
<h2>
<ul>
<?php
try {
$sql = "SELECT somename, some_id FROM products";
$stmt = $dbconn->query($sql);
if($stmt !== false) {
foreach($stmt as $row) { //output just name
echo "<li>";
echo htmlentities($row['somename'])."<br />";
if($stmt !== false) {
$url = "<a href=index.php?q=".
htmlentities($row['some_id'])."/>".
htmlentities($row['somename'])."'s Review</a>";
echo $url; //output URL
echo "</li>"."<br />";
}
else {
echo "Unnecessary";
}
}
if($_GET['url']) { //don't really know what to put here
header("Location: $url"); //can't use headers anyway
}
}
$stmt = null;
}
catch (PDOEXCEPTION $e) {
echo $e->getMessge();
}
?>
</ul>
</h2>
</body>
</html>
You can write URLs as :
http://example.org/index.php/reviews/id/ [where id can be your review id]
and use $_SERVER['PATH_INFO'] in index.php to get part of URL which is after index.php, then explode the text and get desired data out of it.
<?php
$query_string = explode('/', $_SERVER['PATH_INFO']);
switch(count($query_string)) {
case 2:
$some_id = (int) $query_string[1];
if ($some_id === 0) {
//(echos every URI in JSON)
}
else {
// (outputs table some_id info w/ links to reviews)
}
break;
case 3:
//(outputs table column of reviews w/ link to review_ids)
$some_id = (int) $query_string[1];
$table_name = $query_string[2];
break;
case 4:
//(output related column info)
$some_id = (int) $query_string[1];
$table_name = $query_string[2];
$review_id = (int) $query_string[3];
break;
default:
// Error case
}
?>
Try this for size
if (isset($_GET['q']) && !empty($_GET['q']))
{
$params = explode("/",$_GET['q']);
if (isset($params[3]) && !empty($params[3]))
echo "(output {$params[2]} {$params[3]} info)";
else if (isset($params[2]) && !empty($params[2]))
echo "(outputs table column of {$params[2]} w/ link to review_ids)";
else if (isset($params[1]) && !empty($params[1]))
echo "(outputs table {$params[1]} info w/ links to reviews)";
else
echo "(echos every URI in JSON) ";
}
else
echo "(echos dynamically generated URL hyperlinks)";
How do i validate $_GET thats the number coming from correct source.
My url look like : index.php?page=items&catID=5
When users put something like 3 which is doesn't exist on catID. I want it to display error message.
$catID = intval($_GET["catID"]);
if($catID) {
$checkSQL = mysql_query("SELECT * FROM category WHERE category_type='2'");
while($checkROW = mysql_fetch_array($checkSQL)) {
$checkCAT != $checkROW["categoryID"];
echo "err msg";
}
This i can come up so far but it doesn't working as it fire error msg even in correct page.
Thank you
wallk makes a good point, there is a missing if. but if i read this correctly, wouldn't something along the lines of this be more what you are going for? Right now the line:
if($catID) {
is actually only checking if catID (or, catID from the $_GET) is non-zero (not false). My guess if you are looking to check if catID is the categoryID returned from SQL?
$catID = intval($_GET["catID"]);
checkcat($catID);
function checkcat($check_category) {
$checkSQL = mysql_query("SELECT * FROM category WHERE category_type='2'");
while($checkROW = mysql_fetch_array($checkSQL)) {
if ( $check_category != $checkROW["categoryID"] ) {
echo "err msg";
} else {
echo "not an error message";
}
}
}
Expounding on what you are looking for, how about something like this then?
$catID = ($_GET["catID");
if ( !is_numeric($catID) ) {
echo "Not a numeric category!"
} else {
$checkSQLQuery = "SELECT * FROM category WHERE categoryID = '{$catID}' AND category_type='2'"
$resultSQL = mysql_query($checkSQLQuery, $db);
/* NOTE!: Guessing on what your database resouce
pointer is - it isn't included in the origin snippet.
Although, the last opened should be used by default if
this is left out. */
if ( mysql_num_rows($resultSQL) < 1 ) {
echo "Error message, category ID not found"
} else {
echo "Found it!"
}
}
Oh, I see. The first line inside the while loop should have an "if":
while ($checkROW = mysql_fetch_array($checkSQL)) {
if ($checkCAT != $checkROW["categoryID"])
echo "err msg";
It looks like you'll be wanting to use mysql_fetch_assoc(), rather than mysql_fetch_array().